代码实现
package com.git.books.b_design_patterns.n_command; /** * @Description: 命令模式 之 键盘按键命令接口类 * @author: songqinghu * @date: 2017年3月10日 上午11:35:53 * Version:1.0 */ public interface KeyBoardCommand { public void execute(KeyModel model); } package com.git.books.b_design_patterns.n_command; /** * @Description: a 按键命令封装类 命令类中要有执行类的引用 * @author: songqinghu * @date: 2017年3月10日 上午11:37:29 * Version:1.0 */ public class AKeyCommand implements KeyBoardCommand { private ScreenDisplay screenDisplay; public AKeyCommand(ScreenDisplay screenDisplay) { this.screenDisplay=screenDisplay; } @Override public void execute(KeyModel model) { if(KeyModel.Capital.equals(model)){ screenDisplay.printKey("A"); }else{ screenDisplay.printKey("a"); } } } package com.git.books.b_design_patterns.n_command; /** * @Description: a 按键命令封装类 命令类中要有执行类的引用 * @author: songqinghu * @date: 2017年3月10日 上午11:37:29 * Version:1.0 */ public class BKeyCommand implements KeyBoardCommand { private ScreenDisplay screenDisplay; public BKeyCommand(ScreenDisplay screenDisplay) { this.screenDisplay=screenDisplay; } @Override public void execute(KeyModel model) { if(KeyModel.Capital.equals(model)){ screenDisplay.printKey("B"); }else{ screenDisplay.printKey("b"); } } } package com.git.books.b_design_patterns.n_command; /** * @Description: a 按键命令封装类 命令类中要有执行类的引用 * @author: songqinghu * @date: 2017年3月10日 上午11:37:29 * Version:1.0 */ public class CKeyCommand implements KeyBoardCommand { private ScreenDisplay screenDisplay; public CKeyCommand(ScreenDisplay screenDisplay) { this.screenDisplay=screenDisplay; } @Override public void execute(KeyModel model) { if(KeyModel.Capital.equals(model)){ screenDisplay.printKey("C"); }else{ screenDisplay.printKey("c"); } } } package com.git.books.b_design_patterns.n_command; import java.util.ArrayList; import java.util.List; /** * @Description: 键盘命令存储起来,序列的输入。 * @author: songqinghu * @date: 2017年3月10日 下午2:57:55 * Version:1.0 */ public class OrderKeyCommand implements KeyBoardCommand { private List<KeyBoardCommand> orderKeys = new ArrayList<KeyBoardCommand>(); private List<KeyModel> keyModels = new ArrayList<KeyModel>(); @Override public void execute(KeyModel model) { execute(); } public void execute() { for (int i = 0; i < orderKeys.size(); i++) { orderKeys.get(i).execute(keyModels.get(i)); } orderKeys.clear(); keyModels.clear(); } /* * 添加一个输入进入序列中 */ public void add(KeyBoardCommand key,KeyModel model){ orderKeys.add(key); keyModels.add(model); } } package com.git.books.b_design_patterns.n_command; /** * @Description: 屏幕显示操作 * @author: songqinghu * @date: 2017年3月10日 上午11:41:57 * Version:1.0 */ public interface ScreenDisplay { /** * @描述:屏幕打印具体的key * @param key * @return void * @createTime:2017年3月10日 * @author: songqinghu */ public void printKey(String key); } package com.git.books.b_design_patterns.n_command; /** * @Description: 屏幕打印操作 * @author: songqinghu * @date: 2017年3月10日 上午11:43:40 * Version:1.0 */ public class KeyScreenDisplay implements ScreenDisplay { @Override public void printKey(String key) { System.out.println("the screen print : " + key); } } package com.git.books.b_design_patterns.n_command; /** * @Description: 按键模式 大小写 * @author: songqinghu * @date: 2017年3月10日 上午11:38:30 * Version:1.0 */ public enum KeyModel { Capital,Small }
package com.git.books.b_design_patterns.n_command; /** * @Description: 键盘输入调用类 用来调用指定的键盘命令 * @author: songqinghu * @date: 2017年3月10日 下午2:47:51 * Version:1.0 */ public class KeyInputInvoking { private KeyBoardCommand akeyCommand; private KeyBoardCommand bkeyCommand; private KeyBoardCommand ckeyCommand; private OrderKeyCommand orderCommand = new OrderKeyCommand(); public KeyInputInvoking(ScreenDisplay display) { akeyCommand = new AKeyCommand(display); bkeyCommand = new BKeyCommand(display); ckeyCommand = new CKeyCommand(display); } public void printA(KeyModel model){ akeyCommand.execute(model); } public void printB(KeyModel model){ bkeyCommand.execute(model); } public void printC(KeyModel model){ ckeyCommand.execute(model); } public void addSomeKeys(KeyBoardCommand command,KeyModel model){ orderCommand.add(command, model); } public void printSomeKeys(){ orderCommand.execute(); } } package com.git.books.b_design_patterns.n_command; /** * @Description: 命令模式测试类 * @author: songqinghu * @date: 2017年3月10日 下午3:15:43 * Version:1.0 */ public class KeyCommandModelClient { public static void main(String[] args) { KeyInputInvoking invoking = new KeyInputInvoking(new KeyScreenDisplay()); invoking.printA(KeyModel.Capital); invoking.printB(KeyModel.Capital); invoking.printC(KeyModel.Capital); invoking.printA(KeyModel.Small); ScreenDisplay display = new KeyScreenDisplay(); invoking.addSomeKeys(new AKeyCommand(display),KeyModel.Small); invoking.addSomeKeys(new BKeyCommand(display), KeyModel.Capital); invoking.addSomeKeys(new CKeyCommand(display), KeyModel.Small); invoking.printSomeKeys(); } } 结构图:
重点: 掌握命令模式的运行过程及和组合模式的结合使用 参考: <<设计模式>> <<Java与模式>>
:
