java execute方法,有内置的Java类型可以保证execute(T t)方法吗?

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 {

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 implements Executer {

List> executerList;

Handler(List> executer) {

this.executerList = executer;

}

void execute(T t) {

for (Executer 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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值