命令模式是行为模式的一种。行为模式就是关注对象的行为和责任。区别了建造模式、创建模式。
命令模式把一个请求或操作封装在一个对象中,把发出命令的责任和执行命令的责任,委派给不同的对象。
回调就是命令模式的一种.....
例:监听器
main(){
Botton b=new Buttton();
b.addListener(new XXListener(){
void click(Event e){
//..............
}
});
}
一下实现回调
//调用者
public class Invoker{
private Command cmd;
public Invoker(Command cmd){
this.cmd=cmd;
}
public void excute(){
cmd.excute();
}
}
public Interface Command{
public void excute();
}
main(){
Command cmd=new Command(){
public void excute(){
System.out.print("command 执行了......excute");
}
}
Invoker in=new Invoker(cmd);
in.excute();
}