Command模式如何实现Undo操作

Patrick_DK 2002-03-15 10:16:13
我写了一个Command模式的例子,可以编译执行的,但是想继续下去,实现Undo操作,不知道谁可以改改

--------------------------------------------------------------------------
/***********************************************************************
* FileName: CommandPattern5.java
* Module: Command Pattern Example
* Author: Ke Deng
* Created: 2002Äê3ÔÂ14ÈÕ
* Purpose: Undstanding the Command Pattern
***********************************************************************/
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;


public class CommandPattern5
{
public static void main(String[] args)
{
try
{
CommandPattern5 cp=new CommandPattern5();
cp.go();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void go() throws Exception
{
JFrame jFrame=new JFrame("Whole Command Pattern");
// construct button components
CustomButton btnA=new CustomButton("A");
CustomButton btnB=new CustomButton("B");
CustomButton btnC=new CustomButton("C");
CustomButton btnUndo=new CustomButton("Undo");
// construct button events
AButtonCommand btnCmdA=new AButtonCommand(jFrame);
BButtonCommand btnCmdB=new BButtonCommand(jFrame);
CButtonCommand btnCmdC=new CButtonCommand(jFrame);
UndoCommand btnCmdUndo=new UndoCommand();
//
btnA.setCommand(btnCmdA);
btnB.setCommand(btnCmdB);
btnC.setCommand(btnCmdC);
btnUndo.setCommand(btnCmdUndo);
// add action event listener
ActionListener cl=new CustomListener();
btnA.addActionListener(cl);
btnB.addActionListener(cl);
btnC.addActionListener(cl);
btnUndo.addActionListener(cl);
// show the frame
jFrame.getContentPane().setLayout(new FlowLayout());
jFrame.getContentPane().add(btnA);
jFrame.getContentPane().add(btnB);
jFrame.getContentPane().add(btnC);
jFrame.getContentPane().add(btnUndo);
jFrame.setBounds(200,200,300,300);
jFrame.setVisible(true);
}
}


interface Command
{
public void execute();

public void undo();
}


interface CommandHolder
{
public void setCommand(Command cmd);

public Command getCommand();
}


class CustomButton extends JButton implements CommandHolder
{
private Command btnCommand;

public CustomButton(String label)
{
super(label);
}

public void setCommand(Command cmd)
{
btnCommand=cmd;
}

public Command getCommand()
{
return btnCommand;
}
}

class AButtonCommand implements Command
{
private CustomDialog dialogA;

public AButtonCommand(JFrame jf)
{
dialogA=new CustomDialog(jf,"AButtonCommand",100,100,100,100);
}

public void execute()
{
dialogA.showDialog();
}

public void undo()
{
}
}


class BButtonCommand implements Command
{
private CustomDialog dialogB;

public BButtonCommand(JFrame jf)
{
dialogB=new CustomDialog(jf,"BButtonCommand",220,100,100,100);
}

public void execute()
{
dialogB.showDialog();
}

public void undo()
{
}
}


class CButtonCommand implements Command
{
private CustomDialog dialogC;

public CButtonCommand(JFrame jf)
{
dialogC=new CustomDialog(jf,"CButtonCommand",340,100,100,100);
}

public void execute()
{
dialogC.showDialog();
}

public void undo()
{
}
}


class UndoCommand implements Command
{
Vector undoList;

public UndoCommand()
{
undoList=new Vector(); // list of commands to undo
}

public void add(Command cmd)
{
if (!(cmd instanceof UndoCommand))
{
undoList.add(cmd);
}
}

public void execute()
{
int index=undoList.size()-1;
if (index>=0)
{
// get last command executed
Command cmd=(Command)undoList.elementAt(index);
cmd.undo(); // undo it
undoList.remove(index); // and remove from list
}
}

public void undo()
{
}
}


class CustomListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
CommandHolder ch=(CommandHolder)ae.getSource();
ch.getCommand().execute();
}
}


class CustomDialog extends JDialog
{
private int cdx,cdy,width,height;

public CustomDialog(JFrame jf,String title,int x,int y,int w,int h)
{
super(jf,title);
cdx=x;
cdy=y;
width=w;
height=h;
}

public void showDialog()
{
setBounds(cdx,cdy,width,height);
setVisible(true);
}
}
...全文
159 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Patrick_DK 2002-03-15
  • 打赏
  • 举报
回复
再请教一下路人甲:

你设计模式都是看些什么资料学的,怎么学的啊?
我看设计模式的例子基本上都能看懂,可是如果叫我自己写,我很难想到会这么写的
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
to aprim
做project就学会啦。
我也不知道。
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
不熟Rose , 自卑!
aprim 2002-03-15
  • 打赏
  • 举报
回复
hoho...
skyyoung(路人甲):好像很有时间!(搞研究的??)
顺便文一句,要买cmpp的接口实现程序,能多少钱卖??
Patrick_DK 2002-03-15
  • 打赏
  • 举报
回复
OK, I see 了

static是关键性的一点

谢谢路人甲和微电

另:不知道路人甲Rose用的熟吗,有个问题给看看

http://www.csdn.net/expert/topic/577/577335.xml?temp=.3849909
xmvigour 2002-03-15
  • 打赏
  • 举报
回复
skyyoung(路人甲):
谢谢老大的鼓励,不过这里回复,怎么不会自动换行了,真是奇怪!
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
xmvigour(微电)兄讲的对,鼓掌!
Patrick_DK 2002-03-15
  • 打赏
  • 举报
回复
to 路人甲:

我正在看。。。
xmvigour 2002-03-15
  • 打赏
  • 举报
回复
其实 疾风摩郎 已经实现了大部分,但undo的功能竟然要对abc 三个按钮进行控制,就必须有一个全局变量来负责管理这些,因为大家都从command继承,所以可以在它里面设置一个vector来存储,而不是放到UndoCommand 里去!
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
可以吗?
xmvigour 2002-03-15
  • 打赏
  • 举报
回复
kao,晚了一步,差点又copy上去一大段,不可实现的方法和: skyyoung(路人甲) 差不多!
snowredfox 2002-03-15
  • 打赏
  • 举报
回复
Attention...
snowredfox 2002-03-15
  • 打赏
  • 举报
回复
关注,Up
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
/***********************************************************************
* FileName: CommandPattern5.java
* Module: Command Pattern Example
* Author: Ke Deng
* Created: 2002Ä?ÔÂ14ÈÕ
* Purpose: Undstanding the Command Pattern
***********************************************************************/
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;


public class CommandPattern5
{
public static void main(String[] args)
{
try
{
CommandPattern5 cp=new CommandPattern5();
cp.go();
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void go() throws Exception
{
JFrame jFrame=new JFrame("Whole Command Pattern");
// construct button components
CustomButton btnA=new CustomButton("A");
CustomButton btnB=new CustomButton("B");
CustomButton btnC=new CustomButton("C");
CustomButton btnUndo=new CustomButton("Undo");
// construct button events
AButtonCommand btnCmdA=new AButtonCommand(jFrame);
BButtonCommand btnCmdB=new BButtonCommand(jFrame);
CButtonCommand btnCmdC=new CButtonCommand(jFrame);
UndoCommand btnCmdUndo=new UndoCommand();
//
btnA.setCommand(btnCmdA);
btnB.setCommand(btnCmdB);
btnC.setCommand(btnCmdC);
btnUndo.setCommand(btnCmdUndo);
// add action event listener
ActionListener cl=new CustomListener();
btnA.addActionListener(cl);
btnB.addActionListener(cl);
btnC.addActionListener(cl);
btnUndo.addActionListener(cl);
// show the frame
jFrame.getContentPane().setLayout(new FlowLayout());
jFrame.getContentPane().add(btnA);
jFrame.getContentPane().add(btnB);
jFrame.getContentPane().add(btnC);
jFrame.getContentPane().add(btnUndo);
jFrame.setBounds(200,200,300,300);
jFrame.setVisible(true);
}
}


interface Command
{
static Vector undoList = new Vector();
public void execute();

public void undo();
}


interface CommandHolder
{
public void setCommand(Command cmd);

public Command getCommand();
}


class CustomButton extends JButton implements CommandHolder
{
private Command btnCommand;

public CustomButton(String label)
{
super(label);
}

public void setCommand(Command cmd)
{
btnCommand=cmd;
}

public Command getCommand()
{
return btnCommand;
}
}

class AButtonCommand implements Command
{
private CustomDialog dialogA;

public AButtonCommand(JFrame jf)
{
dialogA=new CustomDialog(jf,"AButtonCommand",100,100,100,100);
}

public void execute()
{
undoList.add(this);
System.out.println(undoList.size());
dialogA.showDialog();
}

public void undo()
{
dialogA.dispose();
}
}


class BButtonCommand implements Command
{
private CustomDialog dialogB;

public BButtonCommand(JFrame jf)
{
dialogB=new CustomDialog(jf,"BButtonCommand",220,100,100,100);
}

public void execute()
{
undoList.add(this);
System.out.println(undoList.size());
dialogB.showDialog();
}

public void undo()
{
dialogB.dispose();
}
}


class CButtonCommand implements Command
{
private CustomDialog dialogC;

public CButtonCommand(JFrame jf)
{
dialogC=new CustomDialog(jf,"CButtonCommand",340,100,100,100);
}

public void execute()
{
undoList.add(this);
System.out.println(undoList.size());
dialogC.showDialog();
}

public void undo()
{
dialogC.dispose();
}
}


class UndoCommand implements Command
{
public UndoCommand()
{
//undoList=new Vector(); // list of commands to undo
}

public void add(Command cmd)
{
if (!(cmd instanceof UndoCommand))
{
System.out.println("Add command");
undoList.add(cmd);
}
}

public void execute()
{
int index=undoList.size()-1;
if (index>=0)
{
// get last command executed
Command cmd=(Command)undoList.elementAt(index);
cmd.undo(); // undo it
undoList.remove(index); // and remove from list
}
}

public void undo()
{
}
}


class CustomListener implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
CommandHolder ch=(CommandHolder)ae.getSource();
ch.getCommand().execute();
}
}


class CustomDialog extends JDialog
{
private int cdx,cdy,width,height;

public CustomDialog(JFrame jf,String title,int x,int y,int w,int h)
{
super(jf,title);
cdx=x;
cdy=y;
width=w;
height=h;
}

public void showDialog()
{
setBounds(cdx,cdy,width,height);
setVisible(true);
}
}
Patrick_DK 2002-03-15
  • 打赏
  • 举报
回复
路人甲大虾:

你编译运行我这个程序看看先,就是简单实现了Command模式的一个例子

一共4个按钮,A B C和Undo

按A B C,分别弹出Dialog A, Dialog B, Dialog C

我想实现的Undo操作是
按一次Undo按钮,就把最后一次弹出的Dialog给Dispose掉,要体现Command模式的思想
skyyoung 2002-03-15
  • 打赏
  • 举报
回复
你想UNDO什么?

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧