Command模式如何实现Undo操作
我写了一个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);
}
}