有内置的Java类型可以保证execute(T t)方法吗? [英] Is there a built-in Java type that guarantees an execute(T t) method?

查看:100
本文介绍了有内置的Java类型可以保证execute(T t)方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎对以下类型的需求如此普遍,以至于它应该已经内置在Java中:

It seems the need for a type like the following would be so ubiquitous that something like it should be already built into Java:

public interface Executer<T> {
   void execute(T object);
}

然后可以在其他类中使用它,例如这个琐碎的示例,它在一个对象上调用一堆执行程序.

It can then be used in other classes like this trivial example that calls a bunch of executers on an object.

class Handler<T> implements Executer<T> {
   List<Executer<T>> executerList;

   Handler(List<Executer<T>> executer) {
      this.executerList = executer;
   }

   void execute(T t) {
      for (Executer<T> executer : this.executerList) {
         executer.execute(t);
      }
   }
}

是否存在等效的内置类型或等效的公共库?这个概念有名字吗?

Is there a built-in type equivalent or a common library equivalent? Is there a name for this concept?

推荐答案

我认为该概念的名称是策略模式.有效地封装了算法,这使将一种策略换成另一种策略或应用多种策略变得更加容易.

I think the name of the concept is the strategy pattern. Effectively you are encapsulating an algorithm, and this makes it easier to swap out one strategy for another or to apply a number of strategies.

此设计模式非常容易实现,并且execute方法不需要仅接受特定类型的单个参数.因此,我怀疑您会找到内置的Java类型,但这是一种众所周知的设计模式.

This design pattern is very easy to implement, and the execute method need not take only a single argument of a specific type. As such, I doubt you will find a built-in Java type, but it is a well-known design pattern.

这篇关于有内置的Java类型可以保证execute(T t)方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆