目录
1.Command模式
1.1 作用
1.2 示例程序
1.2.1 示例程序类图
1.2.2 Command接口
1.2.3 MacroCommand类
1.2.4 DrawCommand类
1.2.5 Drawable接口
1.2.6 DrawCanvas类
1.2.7 Main类
1.2.8 示例程序时序图
1.3 Command模式中的角色
1.4 其他特点
1.Command模式
1.1 作用
一个类在进行工作时会调用自己或是其他类的方法,虽然调用结果会反映在对象的状态中,但并不会留下工作的历史记录。Command模式就是用类来表示命令的实例,把命令当成“物”来使用,要想管理工作的历史记录,只需管理这些实例的集合即可,而且还可以随时再次执行过去的命令,或是将多个过去的命令整合为一个新命令并执行。
1.2 示例程序
1.2.1 示例程序类图
1.2.2 Command接口
//表示命令的接口
public interface Command {
public abstract void execute();//执行命令
}
1.2.3 MacroCommand类
//具体的命令类
public class MacroCommand implements Command{
private Stack commands = new Stack<>();//存储历史命令,用栈来体现命令的顺序,栈顶是最近的命令
@Override
public void execute() {
Iterator it = commands.iterator();//把历史命令都执行一遍
while (it.hasNext()) {
((Command)it.next()).execute();
}
}
public void add(Command cmd) {
if (cmd != this) {//防止不小心把当前命令加进去,造成死循环
commands.push(cmd);
}
}
public void undo() {//撤销一个命令
if (!commands.empty()) {
commands.pop();
}
}
public void clear() {//清除全部命令
commands.clear();
}
}
1.2.4 DrawCommand类
//画一个点的命令类
public class DrawCommand implements Command{
private Drawable drawable;//绘制对象
private Point position;//绘制点的位置
public DrawCommand(Drawable drawable, Point position) {
this.drawable = drawable;
this.position = position;
}
@Override
public void execute() {//执行绘制点的操作
drawable.draw(position.x,position.y);
}
}
1.2.5 Drawable接口
//表示绘制对象的接口
public interface Drawable {
public abstract void draw(int x,int y);//绘制
}
1.2.6 DrawCanvas类
//表示绘制对象的具体类,即命令的操作对象
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;
}
public void paint(Graphics g) {//历史命令全部重新绘制
history.execute();
}
@Override
public void draw(int x, int y) {//绘制圆点
Graphics g = getGraphics();
g.setColor(color);
g.fillOval(x - radius,y - radius,radius * 2,radius * 2);
}
}
1.2.7 Main类
//具体的命令类
public class MacroCommand implements Command{
private Stack commands = new Stack<>();//存储历史命令,用栈来体现命令的顺序,栈顶是最近的命令
@Override
public void execute() {
Iterator it = commands.iterator();//把历史命令都执行一遍
while (it.hasNext()) {
((Command)it.next()).execute();
}
}
public void add(Command cmd) {
if (cmd != this) {//防止不小心把当前命令加进去,造成死循环
commands.push(cmd);
}
}
public void undo() {//撤销一个命令
if (!commands.empty()) {
commands.pop();
}
}
public void clear() {//清除全部命令
commands.clear();
}
}
1.2.4 DrawCommand类
//画一个点的命令类
public class DrawCommand implements Command{
private Drawable drawable;//绘制对象
private Point position;//绘制点的位置
public DrawCommand(Drawable drawable, Point position) {
this.drawable = drawable;
this.position = position;
}
@Override
public void execute() {//执行绘制点的操作
drawable.draw(position.x,position.y);
}
}
1.2.5 Drawable接口
//表示绘制对象的接口
public interface Drawable {
public abstract void draw(int x,int y);//绘制
}
1.2.6 DrawCanvas类
//表示绘制对象的具体类,即命令的操作对象
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;
}
public void paint(Graphics g) {//历史命令全部重新绘制
history.execute();
}
@Override
public void draw(int x, int y) {//绘制圆点
Graphics g = getGraphics();
g.setColor(color);
g.fillOval(x - radius,y - radius,radius * 2,radius * 2);
}
}
1.2.7 Main类
//表示绘制对象的接口
public interface Drawable {
public abstract void draw(int x,int y);//绘制
}
1.2.6 DrawCanvas类
//表示绘制对象的具体类,即命令的操作对象
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;
}
public void paint(Graphics g) {//历史命令全部重新绘制
history.execute();
}
@Override
public void draw(int x, int y) {//绘制圆点
Graphics g = getGraphics();
g.setColor(color);
g.fillOval(x - radius,y - radius,radius * 2,radius * 2);
}
}
1.2.7 Main类
GUI的布局:
//设置GUI布局,同时有Main方法
public class Main 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 Main(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();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearButton) {
history.clear();
canvas.repaint();
}
}
@Override
public void mouseDragged(MouseEvent e) {//鼠标点击
Command cmd = new DrawCommand(canvas,e.getPoint());//新建一个绘制圆点的命令
history.add(cmd);//在历史记录中增加最新命令
cmd.execute();//执行命令
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
public static void main(String[] args) {
new Main("Command Pattern Sample");
}
}
输出为:
1.2.8 示例程序时序图
1.3 Command模式中的角色
- Command(命令):该角色负责定义命令的接口API。上述的Command接口。
- ConcreteCommand(具体的命令):该角色负责实现在Command角色中定义的接口API。上述的MacroCommand类和DrawCommand类。
- Receiver(接受者):该角色是Command角色执行命令时的对象,也可以称其为命令接受者。上述的DrawCanvas类接受DrawCommand的命令。
- Client(请求者):该角色负责生成ConcreteCommand角色并分配Receiver角色。上述的Main类。
- Invoker(发动者):该角色是开始执行命令的角色,它会调用在Command角色中定义的接口API。上述的DrawCanvas类和Main类。
- Command模式的类图:
- Command模式的时序图:
1.4 其他特点
- 在示例程序中,MacroCommand类的history实例代表了绘制的历史记录,在该字段中保存了之前所有的绘制信息,也就是说,如果我们将它保存为文件,就可以永久保存历史记录。
根据图解设计模式书籍进行整理。



