【Java设计模式】· 命令模式(Command Pattern)

命令模式:

1.命令模式:按自己的理解,命令模式的优点在于封装性完好,user在使用命令模式时候只需要知道“功能”而不需了解其他,而命令模式的重点在于,把所有的命令事先写好在一个类(ControlPanel)中,这样所有的操作都仅对这个(ControlPanel)类执行。




ICommand:所有命令的父类

ProjecterOnCommand 等:命令的具体类

Light 等:命令的具体动作

ControlPanel:控制类,控制命令的动作



2.例子:简单地模拟控制一间房间的灯的开关。


首先,创建所有命令(command)的父类IComm

package pers.reus.model.command.impl;

public abstract class ICommand {
	//抽象方法Execute用来执行命令
	public abstract void Execute();
}


接下来创建  开灯(LightOnCommand)和关灯(LightOffCommand)两个子类


开灯:

package pers.reus.model.command;

import pers.reus.model.command.impl.ICommand;
import pers.reus.model.concreteCommand.Light;

public class LightOnCommand extends ICommand{
	//灯的具体类Light
	Light light;
	//声明开灯类时,需要对此类设置一个具体的灯类对象Light
	public LightOnCommand(Light light){
		this.light = light;
	}
	//Execute调用灯的具体类里面的"开灯"函数
	public void Execute() {
		light.TurnOnLight();
	}

}


关灯:

<span style="font-size:12px;">package pers.reus.model.command;

import pers.reus.model.command.impl.ICommand;
import pers.reus.model.concreteCommand.Light;

public class LightOffCommand extends ICommand{
	//灯的具体类Light
	Light light;
	//声明关灯类时,需要对此类设置一个具体的灯类对象Light
	public LightOffCommand(Light light){
		this.light = light;
	}
	//Execute调用灯的具体类里面的"关灯"函数
	public void Execute() {
		light.TurnOffLight();
	}
}</span>

接下来创建 灯 的具体类:


<span style="font-size:12px;">package pers.reus.model.concreteCommand;
//灯 的具体类
public class Light {
	//开灯
	public void TurnOnLight(){
		System.out.println("开灯!");
	}
	//关灯
	public void TurnOffLight(){
		System.out.println("关灯!");
	}
}</span>



最重要的是这个控制面板(CommandPanel)类:


<span style="font-size:12px;">package pers.reus.model.controlPanel;

import java.util.Hashtable;

import pers.reus.model.command.impl.ICommand;
//CpControlPanel 里面包括一系列的命令操作
public class CpControlPanel {
	//声明一个Hashtable,用来存放命令代号和命令类
	Hashtable ht = new Hashtable();
	/**
	 * cmd是命令代号
	 * ic是命令类
	 * 如setCommand("lco",LightOnCommand)表示为把"开灯类"存入ht,代号为lco
	 **/
	public void setCommand(String cmd,ICommand ic){
	    ht.put(cmd,ic);
	}
	//为每一个功能都创建特定的函数,调用ExcuteLightOn()就会开灯
	public void ExcuteLightOn(){
		//从ht里面通过代号"lco"找到已经存进去的LightOnCommand类,并赋值给ic
	    ICommand ic = (ICommand) ht.get("lco");
	    //调用LightOnCommand类的Execute()函数
	    ic.Execute();
	}
	public void ExcuteLightOff(){
		//从ht里面通过代号"lcf"找到已经存进去的LightOffCommand类,并赋值给ic
	    ICommand ic = (ICommand) ht.get("lcf");
	    //调用LightOnCommand类的Execute()函数
	    ic.Execute();
	}
}</span> 


客户类进行测试:

package pers.reus.model.client;

import pers.reus.model.command.LightOffCommand;
import pers.reus.model.command.LightOnCommand;
import pers.reus.model.concreteCommand.Light;
import pers.reus.model.controlPanel.CpControlPanel;

//客户类测试
public class CommandPatternClient {

	public static void main(String[] args) {
		//声明灯的具体类
		Light light = new Light();
		//在开灯和关灯类中放入灯的具体对象
		LightOnCommand lco = new LightOnCommand(light);
		LightOffCommand lcf = new LightOffCommand(light);
		//声明控制面板类
		CpControlPanel  ccp = new CpControlPanel();
		//把开灯,关灯的ICommand类放入hashtable并设定对应的代号
		ccp.setCommand("lco",lco);
		ccp.setCommand("lcf",lcf);
		//调用控制面板类(CpContrilPanel)里面的开灯函数,进行测试
		ccp.ExcuteLightOn();

	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值