设计模式之命令模式(Command)

    xiaoxiao2021-03-25  90

    意图: 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作. 适用性: 1.抽象出待执行的动作以参数化某对象. 2.在不同的时刻指定,排列和执行请求. 3.支持取消操作 4.支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍. 5.用构建在原语操作上的高层操作构建一个系统. 效果: 1.command模式将调用操作的对象与知道如何实现该操作的对象解耦. 2.command是头等的对象.它们可像其他的对象一样被操纵和扩展. 3.你可将多个命令装配成一个复合的命令. 4.增加新的command很容易,因为这无需改变已有的类.

    代码实现

    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与模式>>

    :

    转载请注明原文地址: https://ju.6miu.com/read-23591.html

    最新回复(0)