ZKX's LAB

天天聊设计模式,你真的会使用命令模式吗?

2020-07-22新闻17

设计模式

设计模式在我们日常开发过程中经常被用到,很多人也都知道,它可以说是经过许多人反复实战、修改,代码设计经验的总结,使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

大家都知道java设计模式总共有23种,那可以分几大类呢?

从功能性上大致可以分为三类:创建型模式、结构型模式、行为型模式。

创建型模式:主要用于创建对象

主要包括:简单工厂(Simple Factory)模式、抽象工厂(Factory Method)模式、单例(Singleton)模式、建造者(Builder)模式和原型(Prototype)模式共5种。

结构型模式:主要用于处理类或者对象的组合

主要包括适配器模式(Adapter)、装饰(Decorator)模式、代理(Proxy)模式、外观/门面(Facade)模式、桥接(Bridge)模式、组合/合成(Composite)模式和享元(Flyweight Pattern)模式共7种。

行为型模式:主要用于描述类或者对象怎样交互和怎样给分配职责

主要包括:策略(Strategy)模式、模板方法(Templete Method)模式、观察者(Observer)模式、迭代子(Iterator)模式、责任链(Chain of Responsibility)模式、命令(Command)模式、备忘录(Memento)模式、状态(State)模式、访问者(Visitor)模式、中介者/调停者(Mediator)模式和解释器(Interpreter)模式共11种。

今天就带大家聊聊其中一种设计模式:命令模式。命令模式

命令模式是一种数据驱动设计模式,它属于行为型模式,请求以命令的形式包裹在对象中,并传给调用对象,调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象。创建命令模式UML类图

创建命令模式的步骤

(1):创建命令接口Command

(2):创建命令接收者LightReceiver

(3):创建实现类LightOffCommand、LightOnCommand、NoCommand

(4):创建命令操作者RemoteController

创建命令接口Command/** * @author YLY * @ClassName Command * @Date 2020/7/21 * @Version 1.0.2 */public interface Command { /** * 执行动作(操作) */ void execute(); /** * 撤销动作(操作) */ void undo();}

创建命令接收者LightReceiver/** * @author YLY * @ClassName LightReceiver * @Date 2020/7/21 * @Version 1.0.2 */public class LightReceiver { public void on() { System.out.println("电灯打开了。。。"); } public void off() { System.out.println("电灯关闭了。。。"); }}

创建实现类LightOffCommand、LightOnCommand、NoCommand/** * @author YLY * @ClassName LightOffCommand * @Date 2020/7/21 * @Version 1.0.2 */public class LightOffCommand implements Command { /** * 聚合LightReceiver */ private LightReceiver lightReceiver; public LightOffCommand(LightReceiver lightReceiver) { this.lightReceiver = lightReceiver; } @Override public void execute() { // 调用接受者的方法 lightReceiver.off(); } @Override public void undo() { // 调用接受者的方法 lightReceiver.on(); }}/** * @author YLY * @ClassName LightOnCommand * @Date 2020/7/21 * @Version 1.0.2 */public class LightOnCommand implements Command { /** * 聚合LightReceiver */ private LightReceiver lightReceiver; public LightOnCommand(LightReceiver lightReceiver) { this.lightReceiver = lightReceiver; } @Override public void execute() { // 调用接受者的方法 lightReceiver.on(); } @Override public void undo() { // 调用接受者的方法 lightReceiver.off(); }}/** * @author YLY * @ClassName NoCommand 空命令用于初始化,防止空指针 * @Date 2020/7/21 * @Version 1.0.2 */public class NoCommand implements Command { @Override public void execute() { } @Override public void undo() { }}

创建命令操作者RemoteController/** * @author YLY * @ClassName RemoteController * @Date 2020/7/21 * @Version 1.0.2 */public class RemoteController { /** * 开 按钮的命令数组 */ Command[] onCommands; Command[] offCommands; /** * 执行撤销的命令 */ Command undoCommand; /** * 初始化RemoteController */ public RemoteController() { onCommands = new Command[5]; offCommands = new Command[5]; for (int i = 0; i < 5; i++) { onCommands[i] = new NoCommand(); offCommands[i] = new NoCommand(); } } /** * 给按钮设置命令 * * @param no 按钮 * @param onCommand * @param offCommand */ public void setCommand(int no, Command onCommand, Command offCommand) { onCommands[no] = onCommand; offCommands[no] = offCommand; } /** * 按下开按钮 */ public void onButton(int no) { // 执行本次按下的按钮操作 onCommands[no].execute(); // 记录本次操作用于撤回 undoCommand = onCommands[no]; } /** * 按下关按钮 */ public void offButton(int no) { // 执行本次按下的按钮操作 offCommands[no].execute(); // 记录本次操作用于撤回 undoCommand = offCommands[no]; } /** * 按下撤销按钮 */ public void undoButton() { undoCommand.undo(); }}

使用Cient类来接受和执行命令/** * @author YLY * @ClassName Client * @Date 2020/7/21 * @Version 1.0.2 */public class Client { public static void main(String[] args) { // 创建电灯对象 LightReceiver lightReceiver = new LightReceiver(); // 创建电灯相关的开关命令 LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver); LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver); //创建遥控器 RemoteController remoteController = new RemoteController(); remoteController.setCommand(0, lightOnCommand, lightOffCommand); System.out.println("按下灯的按钮,操作电灯"); remoteController.onButton(0); remoteController.offButton(0); remoteController.undoButton(); }}

执行结果按下灯的按钮,操作电灯电灯打开了。。。电灯关闭了。。。电灯打开了。。。

PS:命令模式在spring的jdbcTemplate中运用,具体的实现方法是query()方法中的StatementCallback接口。

#技术编程

随机阅读

qrcode
访问手机版