命令模式

来源:互联网 发布:国联证券软件下载 编辑:程序博客网 时间:2024/05/24 06:17
public interface Command{void process(int[] target);}public class ProcessArray{public void process(int[] target,Command cmd){cmd.process(target);}}public class CommandTest{public static void main(){ProcessArray pa=new ProcessArray ();int[] target=new {1,2,3,4};pa.process(target,new PrintCommand());pa.process(target,new AddCommand());}}public class PrintCommand implements Command{public void process(int[] target){//..........}}public class AddCommand implements Command{public void process(int[] target){//..........}}

通过一个command接口让ProcessArray类和具体的 “处理行为分离”,只有等到调用ProcessArray的process方法时,传入一个真正的Command实例,才确定对数组的处理行为

0 0