public class Receiver {
public void action(){
System.out.print("command");
}
}
/**
* Created by Administrator on 2017-10-11.
* 命令接口
*/
public interface Command {
public void exe();
}
/**
* Created by Administrator on 2017-10-11.
* 命令类实现了Command接口
*/
public class MyCommand implements Command {
private Receiver receiver;
public MyCommand(Receiver receiver){
this.receiver=receiver;
}
@Override
public void exe() {
receiver.action();
}
}
/**
* Created by Administrator on 2017-10-11.
* 调用者(司令)
*/
public class Invoker {
private Command command;//命令
public Invoker(Command command){
this.command=command;
}
//动作
public void action(){
command.exe();
}
}
/**
* 命令模式(Command)
*/
public class CommandMethodActivity extends AppCompatActivity {
/**
* 司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,
* 传到了士兵耳朵里,士兵去执行。这个过程好在,三者相互解耦,任何一方都不用去依赖其他人,
* 只需要做好自己的事儿就行,司令员要的是结果,不会去关注到底士兵是怎么实现的。
* @param savedInstanceState
*/
// Invoker是调用者(司令员),Receiver是被调用者(士兵),MyCommand是命令,
// 实现了Command接口,持有接收对象,
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_command_method);
Receiver receiver=new Receiver();
Command cmd=new MyCommand(receiver);
Invoker invoker=new Invoker(cmd);
invoker.action();
}
}
github地址:https://github.com/zyj18410105150/DesignMode