频道栏目
首页 > 资讯 > Java > 正文

Java开发设计模式之命令模式解析

18-06-13        来源:[db:作者]  
收藏   我要投稿

Java开发设计模式之命令模式解析。

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;
	}
	
	
}
相关TAG标签
上一篇:C语言程序设计练习之2007 平方和与立方和
下一篇:C# 有序序列 SortedList解析
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站