设计模式学习笔记——命令(Command)模式
设计模式学习笔记——命令(Command)模式
@(设计模式)[设计模式, 命令模式, command]
- 设计模式学习笔记命令Command模式
- 基本介绍
- 命令案例
- 类图
- 实现代码
- Command接口
- MacroCommand类
- Drawable接口
- DrawCommand类
- DrawCanvas类
- 测试类
- 运行结果
- 命令模式中的角色
- Command命令
- ConcreteCommand具体的命令
- Receiver接收者
- Client请求者
- Invoker发动者
- 类图
- 时序图
基本介绍
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
命令案例
类图
实现代码
Command接口
package com.pc.command.example.command;/*** 命令接口* Created by Switch on 2017/3/31.*/
public interface Command {/*** 执行*/void execute();
}
MacroCommand类
package com.pc.command.example.command;import java.util.Iterator;
import java.util.Stack;/*** 大量命令类* Created by Switch on 2017/3/31.*/
public class MacroCommand implements Command {/*** 命令的集合*/private Stack<Command> commands = new Stack<>();@Overridepublic void execute() {Iterator<Command> it = commands.iterator();while (it.hasNext()) {it.next().execute();}}/*** 添加命令** @param cmd 命令*/public void append(Command cmd) {if (cmd != this) {commands.push(cmd);}}/*** 删除最后一条命令*/public void undo() {if (!commands.empty()) {commands.pop();}}/*** 删除所有命令*/public void clear() {commands.clear();}
}
Drawable接口
package com.pc.command.example.drawer;/*** 绘图接口*/
public interface Drawable {/*** 绘制** @param x x坐标* @param y y坐标*/void draw(int x, int y);
}
DrawCommand类
package com.pc.command.example.drawer;import com.pc.command.example.command.Command;import java.awt.*;/*** 绘图命令类*/
public class DrawCommand implements Command {/*** 绘制对象*/protected Drawable drawable;/*** 绘制位置*/private Point position;public DrawCommand(Drawable drawable, Point position) {this.drawable = drawable;this.position = position;}@Overridepublic void execute() {drawable.draw(position.x, position.y);}
}
DrawCanvas类
package com.pc.command.example.drawer;import com.pc.command.example.command.MacroCommand;import java.awt.*;/*** 绘制面板类*/
public class DrawCanvas extends Canvas implements Drawable {/*** 颜色*/private Color color = Color.red;/*** 要绘制的圆点的半径*/private int radius = 6;/*** 命令的历史记录*/private MacroCommand history;public DrawCanvas(int width, int height, MacroCommand history) {setSize(width, height);setBackground(Color.white);this.history = history;}@Overridepublic void paint(Graphics g) {history.execute();}@Overridepublic void draw(int x, int y) {Graphics g = getGraphics();g.setColor(color);g.fillOval(x - radius, y - radius, radius * 2, radius * 2);}
}
测试类
package com.pc.command.example.test;import com.pc.command.example.command.Command;
import com.pc.command.example.command.MacroCommand;
import com.pc.command.example.drawer.DrawCanvas;
import com.pc.command.example.drawer.DrawCommand;import javax.swing.*;
import java.awt.event.*;/*** MacroCommand Tester.** @author Switch* @version 1.0*/
public class CommandTest extends JFrame implements ActionListener, MouseMotionListener, WindowListener {// 绘制的历史记录private MacroCommand history = new MacroCommand();// 绘制区域private DrawCanvas canvas = new DrawCanvas(400, 400, history);// 删除按钮private JButton clearButton = new JButton("clear");// 构造函数public CommandTest(String title) {super(title);this.addWindowListener(this);canvas.addMouseMotionListener(this);clearButton.addActionListener(this);Box buttonBox = new Box(BoxLayout.X_AXIS);buttonBox.add(clearButton);Box mainBox = new Box(BoxLayout.Y_AXIS);mainBox.add(buttonBox);mainBox.add(canvas);getContentPane().add(mainBox);pack();show();}// ActionListener接口中的方法public void actionPerformed(ActionEvent e) {if (e.getSource() == clearButton) {history.clear();canvas.repaint();}}// MouseMotionListener接口中的方法public void mouseMoved(MouseEvent e) {}public void mouseDragged(MouseEvent e) {Command cmd = new DrawCommand(canvas, e.getPoint());history.append(cmd);cmd.execute();}// WindowListener接口中的方法public void windowClosing(WindowEvent e) {System.exit(0);}public void windowActivated(WindowEvent e) {}public void windowClosed(WindowEvent e) {}public void windowDeactivated(WindowEvent e) {}public void windowDeiconified(WindowEvent e) {}public void windowIconified(WindowEvent e) {}public void windowOpened(WindowEvent e) {}public static void main(String[] args) {new CommandTest("Command Pattern Sample");}
}
运行结果
命令模式中的角色
Command(命令)
Command
角色负责定义命令的接口(API
)。在案例中,由Command
接口扮演此角色。
ConcreteCommand(具体的命令)
ConcreteCommand
角色负责实现在Command
角色中定义的接口(API
)。在案例中,由MacroCommand
类和DrawCommand
类扮演此角色。
Receiver(接收者)
Receiver
角色是Command
角色执行命令时的对象,也可以称其为命令接收者。在案例中,由DrawCanvas
类接收DrawCanvas
类和DrawCommand
类扮演此角色。
Client(请求者)
Client
角色负责生成ConcreteCommand
角色并分配Receiver
角色。在案例中,由测试类扮演此角色。在响应鼠标拖拽事件时,它生成了DrawComrnand
类的实例,并将扮演Receiver
角色的DrawCanvas
类的实例传递给了DrawCommand
类的构造函数。
Invoker(发动者)
Invoker
角色是开始执行命令的角色,它会调用在Command
角色中定义的接口(API
)。在案例中,由测试和DrawCanvas
类扮演此角色。这两个类都调用了Command
接口中的execute
方法。测试类同时扮演了Client
角色和Invoker
角色。
类图
时序图
GitHub:DesignPatternStudy
——————参考《图解设计模式》
设计模式学习笔记——命令(Command)模式相关推荐
- 设计模式学习笔记——解释器(Interpreter)模式
设计模式学习笔记--解释器(Interpreter)模式 @(设计模式)[设计模式, 解释器模式, Interpreter] 设计模式学习笔记解释器Interpreter模式 基本介绍 解释器案例 类 ...
- 设计模式学习笔记——代理(Proxy)模式
设计模式学习笔记--代理(Proxy)模式 @(设计模式)[设计模式, 代理模式, proxy] 设计模式学习笔记代理Proxy模式 基本介绍 代理案例 类图 实现代码 Printable接口 Pri ...
- 设计模式学习笔记——状态(State)模式框架
设计模式学习笔记--状态(State)模式框架 @(设计模式)[设计模式, 状态模式, State] 设计模式学习笔记状态State模式框架 基本介绍 状态案例 类图 实现代码 State接口 Day ...
- 设计模式学习笔记——备忘录(Memento)模式
设计模式学习笔记--备忘录(Memento)模式 @(设计模式)[设计模式, 备忘录模式, memento] 设计模式学习笔记备忘录Memento模式 基本介绍 备忘录案例 类图 实现代码 Memen ...
- 设计模式学习笔记——观察者(Observer)模式
设计模式学习笔记--观察者(Observer)模式 @(设计模式)[设计模式, 观察者模式, Observer] 设计模式学习笔记观察者Observer模式 基本介绍 观察者案例 类图 实现代码 Ob ...
- 设计模式学习笔记——外观(Facade)模式
设计模式学习笔记--外观(Facade)模式 @(设计模式)[设计模式, 外观模式, facade] 设计模式学习笔记外观Facade模式 基本介绍 外观案例 类图 实现代码 Database类 ma ...
- 设计模式学习笔记——访问者(Visitor)模式
设计模式学习笔记--访问者(Visitor)模式 @(设计模式)[设计模式, 访问者模式, visitor] 设计模式学习笔记访问者Visitor模式 基本介绍 访问者案例 类图 实现代码 Visit ...
- 设计模式学习笔记——装饰(Decorator)模式
设计模式学习笔记--装饰(Decorator)模式 @(设计模式)[设计模式, 装饰模式, decorator] 设计模式学习笔记装饰Decorator模式 基本介绍 装饰案例 类图 实现代码 Dis ...
- 设计模式学习笔记——组合(Composite)模式
设计模式学习笔记--组合(Composite)模式 @(设计模式)[设计模式, 组合模式, composite] 设计模式学习笔记组合Composite模式 基本介绍 组合案例 类图 实现代码 Ent ...
最新文章
- 受益匪浅:十个哲理寓言,十个成功秘诀
- python中requests库的用途-python中requests库的post请求
- git2.29.2.2怎么安装_制作Win10安装U盘时install.wim大于4G怎么办?
- 数据库中的乐观锁与悲观锁详解
- 【深度学习】深度学习经典数据集汇总
- 35所大学获批新增「人工智能」本科专业,工学学位、四年制
- 外星人跑深度学习_上海港汇外星人店,51M2020开光追和DLSS2.0畅玩《赛博朋克2077》...
- java 操作redis
- libsvm中数据归一化的重要性
- Linux编程训练网站,OK6410汇编程序练习
- python高斯求和_利用Python进行数据分析(3)- 列表、元组、字典、集合
- 安卓源代码_如何从在安卓Android手机获取微信小程序源代码
- centos7安装禅道
- Java【递归及过滤器】
- 将淘宝客数据导入自己的数据库
- CAA-几何图形集下直接添加参数
- Python 自动清理电脑垃圾文件,一键启动即可
- php授权微信自动扣款,【微信支付】微信代扣开发者文档
- 新款MacBook M2芯片 安装python
- Java基础系列(三十):局部内部类