JavaCommand模式

本文最后更新于:1 年前

命令也是类

1 - 表示”命令”的接口

public interface Command {
    public abstract void execute();
}

2 - 表示”由多条命令整合成的命令”的类

public class MacroCommand implements Command{
    // 命令的集合
    private Stack<Command> commands = new Stack<Command>();

    // 执行
    @Override
    public void execute() {
        for (Object command : commands) {
            ((Command) command).execute();
        }
    }

    // 添加命令
    public void append(Command cmd) {
        if (cmd != this) {
            commands.push(cmd);
        }
    }

    // 删除最后一条命令
    public void undo() {
        if (!commands.empty()) {
            commands.pop();
        }
    }

    // 删除所有命令
    public void clear() {
        commands.clear();
    }
}

3 - 表示”绘制一个点的命令”的类

public class DrawCommand implements Command {
    // 绘制对象
    protected 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);
    }

}

4 - 示”绘制对象”的类

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

5 - 实现”绘制对象”的类

public class DrawCanvas extends Canvas implements Drawable{
    // 颜色
    private Color color = Color.gray;
    // 命令的历史记录
    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);
        // 要绘制圆点的半径
        int radius = 6;
        g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
    }
}

6 - 测试程序行为的类

public class Main extends JFrame implements ActionListener, MouseMotionListener, WindowListener{
    // 绘制的历史记录
    private MacroCommand history = new MacroCommand();
    // 绘制区域
    private DrawCanvas canvas = new DrawCanvas(800, 450, 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();
    }

    // ActionListener接口中的方法
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == clearButton) {
            history.clear();
            canvas.repaint();
        }
    }

    // MouseMotionListener接口中的方法
    @Override
    public void mouseDragged(MouseEvent e) {
        Command cmd = new DrawCommand(canvas, e.getPoint());
        history.append(cmd);
        cmd.execute();
    }

    @Override
    public void mouseMoved(MouseEvent mouseEvent) {

    }

    // WindowListener接口中的方法
    @Override
    public void windowOpened(WindowEvent windowEvent) {

    }

    @Override
    public void windowClosing(WindowEvent windowEvent) {
        System.exit(0);
    }

    @Override
    public void windowClosed(WindowEvent windowEvent) {

    }

    @Override
    public void windowIconified(WindowEvent windowEvent) {

    }

    @Override
    public void windowDeiconified(WindowEvent windowEvent) {

    }

    @Override
    public void windowActivated(WindowEvent windowEvent) {

    }

    @Override
    public void windowDeactivated(WindowEvent windowEvent) {

    }

    public static void main(String[] args) {
        new Main("Command Pattern Sample");
    }
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!