Java 设计模式 之 命令模式

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

http://www.verejava.com/?id=16999074395365

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值