每日学一个设计模式22——命令模式

命令模式(命令也是类)

用处

请求以命令的形式包裹在对象中,并传给调用对象。调用对象寻找可以处理该命令的合适的对象,并把该命令传给相应的对象,该对象执行命令。

角色

  • Command(命令)
    该角色负责定义命令的接口(API)
  • ConcreteCommand(具体的命令)
    该角色负责实现Command角色中定义的接口(API)。
  • Recevier(接收者)
    该角色是Command角色执行命令时的对象,也可以称其为命令接收者。
  • Client(请求者)
    该角色负责生成ConcreteCommand并分配Receiver角色。
  • Invoker(发动者)
    该角色是开始执行命令的角色,它会调用在Command角色中定义的接口(API)。

类图

在这里插入图片描述
由类图可知

  • Invoker通过Command接口聚合了ConcreteCommand,在ConcreteCommand中又聚合Receiver
  • Invoker调用ConcreteCommand中的execute方法发出命令,ConcreteCommand再调用Receiver的action方法接受命

举例

//Invoker和Client角色
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();
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == clearButton){
            history.clear();
            canvas.repaint();
        }
    }

    public void mouseMoved(MouseEvent e){
    }
    public void mouseDragged(MouseEvent e){
        Command cmd = new DrawCommand(canvas,e.getPoint());
        history.append(cmd);
        cmd.execute();
    }
    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 Main("Command Pattern Sample");
    }
}
//Command
interface Command{
    void execute();
};

//ConcreteCommand
class MacroCommand implements Command{
    //命令的集合
    private Stack commands = new Stack();
    public void execute(){
        Iterator it = commands.iterator();
        while(it.hasNext()){
            ((Command)it.next()).execute();
        }
    }
    //添加命令
    public void append(Command cmd){
        if(cmd != this){
            commands.push(cmd);
        }
    }

    //删除最后一条命令
    public void clear(){
        commands.clear();
    }
}

//ConcreteCommand角色
class DrawCommand implements Command{
    //绘制对象
    protected Drawable drawable;
    //绘制位置
    protected Point position;

    public DrawCommand(Drawable drawable,Point position){
        this.drawable = drawable;
        this.position = position;
    }

    public void execute(){
        drawable.draw(position.x,position.y);
    }
}

interface Drawable{
    void draw(int x,int y);
}

//Receiver角色
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();
    }
    public void draw(int x,int y){
        Graphics g = getGraphics();
        g.setColor(color);
        g.fillOval(x-radius,y-radius,radius*2,radius*2);
    }
}

在这里插入图片描述

总结

  • 符合开闭原则
  • 实现行为请求者-行为实现者之间的解耦
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑白程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值