http://www.verejava.com/?id=16999074395365
package com.command.theory;
import java.util.ArrayList;
public class TestCommand
{
public static void main(String[] args)
{
Button btnOk=new Button("确定");
Button btnCancel=new Button("取消");
btnOk.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("确定按钮被点击");
}
});
btnCancel.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
System.out.println("取消按钮被点击");
}
});
Mouse m=new Mouse();
m.click(btnOk);
m.click(btnCancel);
}
}
package com.command.theory;
public class Mouse
{
public void click(Button btn)
{
ActionEvent e=new ActionEvent(btn.getText(),btn);
btn.getActionListener().actionPerformed(e);
}
}
package com.command.theory;
public class Button
{
private ActionListener actionListener;
private String text;
public Button(String text)
{
super();
this.text = text;
}
public ActionListener getActionListener()
{
return actionListener;
}
public void addActionListener(ActionListener actionListener)
{
this.actionListener = actionListener;
}
public String getText()
{
return text;
}
public void setText(String text)
{
this.text = text;
}
}
package com.command.theory;
public interface ActionListener
{
public void actionPerformed(ActionEvent e);
}
package com.command.theory;
public class ActionEvent
{
private String name;
private Object source;
public ActionEvent(String name, Object source)
{
super();
this.name = name;
this.source = source;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Object getSource()
{
return source;
}
public void setSource(Object source)
{
this.source = source;
}
}