博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
二十二、Command 命令模式
阅读量:6827 次
发布时间:2019-06-26

本文共 3416 字,大约阅读时间需要 11 分钟。

原理:

时序图:

代码清单:

command.Command

public interface Command {    void execute();}

command.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 append(Command cmd){        if(cmd!=null){            commands.push(cmd);        }    }    public void undo(){        if(!commands.empty()){            commands.pop();        }    }    public void clear(){        commands.clear();    }}

drawer.Drawable

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

drawer.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;    }    //绘制    @Override    public void draw(int x, int y) {       Graphics g = getGraphics();       g.setColor(color);       g.fillOval(x-radius,y-radius,radius*2,radius*2);    }    public void point(Graphics g){        history.execute();    }}

drawer.DrawCommand

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);    }}

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();    }    public static void main(String[] args){        new Main("命令模式");    }    @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.append(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) {    }}

 

转载于:https://www.cnblogs.com/baizhuang/p/10559949.html

你可能感兴趣的文章
在.NET下如何实现密码Hash化
查看>>
缩略图不变形
查看>>
【计算机视觉必读干货】图像分类、定位、检测,语义分割和实例分割方法梳理...
查看>>
SSIS Execute SQL Task 用法
查看>>
使用枚举和结构输出日期
查看>>
面试题:单词翻转(代码简洁&效率)
查看>>
使用oledb读写excel出现“操作必须使用一个可更新的查询”的解决办法
查看>>
Windows Azure Cloud Service (11) PaaS之Web Role, Worker Role(上)
查看>>
OEA 中 WPF 树型表格虚拟化设计方案
查看>>
《Android深度探索(卷1):HAL与驱动开发》虚拟实验环境(Ubuntu Linux)免费下载,不需要CPU虚拟化支持...
查看>>
linux 终端提示符
查看>>
C# 实现多线程的同步方法详解
查看>>
[转贴]当前Java开发中的若干问题
查看>>
基于MapGuide的在线WebGIS站点介绍
查看>>
我 Git 命令列表 (2)【转】
查看>>
[Android Pro] sqlite数据库的char,varchar,text,nchar,nvarchar,ntext的区别
查看>>
SharePoint 2013 开发——开发并部署第一个APP
查看>>
mysql if对数据进行处理 having对数据进行查询 thinkphp中的exp支持更复杂的where查询...
查看>>
MongoDB学习笔记(四)基本管理命令
查看>>
WNFORM实现窗体的DataGrid控件刷新
查看>>