• JAVA设计模式学习笔记-命令模式


    案例一:遥控器
    传统设计方案

    Control

    package com.java.test.commandmode;
    
    public interface Control {
    
    	public void onButton(int slot);
    
    	public void offButton(int slot);
    	
    	public void undoButton();
    }
    

    Light

    package com.java.test.commandmode.device;
    
    public class Light {
    
    	String loc = "";
    
    	public Light(String loc) {
    		this.loc = loc;
    	}
    
    	public void On() {
    
    		System.out.println(loc + " On");
    	}
    
    	public void Off() {
    
    		System.out.println(loc + " Off");
    	}
    
    }
    
    

    Stereo

    package com.java.test.commandmode.device;
    
    public class Stereo {
    	static int volume = 0;
    
    	public void On() {
    		System.out.println("Stereo On");
    	}
    
    	public void Off() {
    		System.out.println("Stereo Off");
    	}
    
    	public void SetCd() {
    		System.out.println("Stereo SetCd");
    	}
    
    	public void SetVol(int vol) {
    		volume = vol;
    		System.out.println("Stereo volume=" + volume);
    	}
    
    	public int GetVol() {
    		return volume;
    	}
    
    	public void Start() {
    		System.out.println("Stereo Start");
    	}
    }
    
    

    TraditionControl

    package com.java.test.commandmode;
    
    import com.java.test.commandmode.device.Light;
    import com.java.test.commandmode.device.Stereo;
    
    public class TraditionControl implements Control {
    	Light light;
    	Stereo stereo;
    
    
        //传统方式存在的问题
        //如果有新的家电要加进来,这里要再写过局部变量
        //构造函数里也要修改
    	public TraditionControl(Light light, Stereo stereo) {
    		this.light = light;
    		this.stereo = stereo;
    	}
    
    	@Override
    	public void onButton(int slot) {
    		// TODO Auto-generated method stub
    		switch (slot) {
    		case 0:
    			light.On();
    			break;
    		case 1:
    			stereo.On();
    			break;
    		case 2:
    			int vol = stereo.GetVol();
    			if (vol < 11) {
    				stereo.SetVol(++vol);
    			}
    			break;
    		}
    	}
    
    	@Override
    	public void offButton(int slot) {
    		// TODO Auto-generated method stub
    		switch (slot) {
    		case 0:
    			light.Off();
    			break;
    		case 1:
    			stereo.Off();
    			break;
    		case 2:
    			int vol = stereo.GetVol();
    			if (vol > 0) {
    				stereo.SetVol(--vol);
    			}
    			break;
    		}
    	}
    
    	@Override
    	public void undoButton() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	
    
    }
    
    

    测试类

    package com.java.test.commandmode;
    
    import com.java.test.commandmode.device.Light;
    import com.java.test.commandmode.device.Stereo;
    
    public class ControlTest {
    	public static void main(String[] args) {
    		Control ctl;
    		Light light = new Light("Bedroom");
    		Stereo stereo = new Stereo();
    		ctl = new TraditionControl(light, stereo);
    
    		ctl.onButton(0);
    		ctl.offButton(0);
    		ctl.onButton(1);
    		ctl.onButton(2);
    		ctl.offButton(2);
    
    		ctl.offButton(1);
    	}
    }
    
    

    命令模式
    原理

    使得命令的请求者和执行者解耦

    Command接口

    package com.java.test.commandmode.command;
    
    public interface Command {
    	public void execute();
    	public void undo();
    }
    

    两个命令类的例子

    LightOnCommand

    package com.java.test.commandmode.command;
    
    import com.java.test.commandmode.device.Light;
    
    public class LightOnCommand implements Command {
    	private Light light;
    	
    	public LightOnCommand(Light light)
    	{
    		this.light=light;
    		
    	}
    	@Override
    	public void execute() {
    		// TODO Auto-generated method stub
    		light.On();
    	}
    
    	@Override
    	public void undo() {
    		// TODO Auto-generated method stub
    		light.Off();
    	}
    
    }
    
    

    StereoOnCommand

    package com.java.test.commandmode.command;
    
    import com.java.test.commandmode.device.Stereo;
    
    public class StereoOnCommand implements Command {
    	private Stereo setreo;
    	public StereoOnCommand(Stereo setreo)
    	{
    		this.setreo=setreo;
    	}
    	
    	@Override
    	public void execute() {
    		// TODO Auto-generated method stub
    		setreo.On();
    		setreo.SetCd();
    		
    	}
    
    	@Override
    	public void undo() {
    		// TODO Auto-generated method stub
    		setreo.Off();
    	}
    
    }
    
    

    NoCommand

    package com.java.test.commandmode.command;
    
    public class NoCommand implements Command {
    
    	@Override
    	public void execute() {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void undo() {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    
    

    CommandModeControl

    package com.java.test.commandmode.command;
    
    import java.util.Stack;
    
    import com.java.test.commandmode.Control;
    import com.java.test.commandmode.device.Light;
    import com.java.test.commandmode.device.Stereo;
    
    public class CommandModeControl implements Control{
    	private Command[] onCommands;
    	private Command[] offCommands;
    	private Stack<Command> stack=new Stack<Command>();
    	public CommandModeControl()
    	{
    		onCommands=new Command[5];
    		 offCommands=new Command[5];
    		
    		 Command noCommand=new NoCommand();
    
    		 //初始化
    		 for(int i=0,len=onCommands.length;i<len;i++)
    		 {
    			 onCommands[i]=noCommand;
    			 offCommands[i]=noCommand;
    		 }
    		 
    	}
    	
    	public void setCommand(int slot,Command onCommand,Command offCommand)
    	{
    		onCommands[slot]=onCommand;
    		 offCommands[slot]=offCommand;
    		
    	}
    
    	@Override
    	public void onButton(int slot) {
    		
    		onCommands[slot].execute();
    		stack.push(onCommands[slot]);
    	}
    
    	@Override
    	public void offButton(int slot) {
    		
    		offCommands[slot].execute();
    		stack.push(offCommands[slot]);
    	}
    
    	@Override
    	public void undoButton() {
    		// TODO Auto-generated method stub
    		stack.pop().undo();
    	}
    
    }
    
    

    测试类

    package com.java.test.commandmode.command;
    
    import com.java.test.commandmode.Control;
    import com.java.test.commandmode.TraditionControl;
    import com.java.test.commandmode.device.Light;
    import com.java.test.commandmode.device.Stereo;
    
    public class ControlTest {
    
    	public static void main(String[] args) {
    		CommandModeControl control = new CommandModeControl();
    		MarcoCommand onmarco,offmarco;
    		Light bedroomlight = new Light("BedRoom");
    		Light kitchlight = new Light("Kitch");
    		Stereo stereo = new Stereo();
    		
    		
    		
    		LightOnCommand bedroomlighton = new LightOnCommand(bedroomlight);
    		LightOffCommand bedroomlightoff = new LightOffCommand(bedroomlight);
    		LightOnCommand kitchlighton = new LightOnCommand(kitchlight);
    		LightOffCommand kitchlightoff = new LightOffCommand(kitchlight);
    
    		 Command[] oncommands={bedroomlighton,kitchlighton};
    		 Command[] offcommands={bedroomlightoff,kitchlightoff};
    			
    		onmarco=new MarcoCommand(oncommands);
    		offmarco=new MarcoCommand(offcommands);
    		
    		StereoOnCommand stereoOn = new StereoOnCommand(stereo);
    		StereoOffCommand stereoOff = new StereoOffCommand(stereo);
    		StereoAddVolCommand stereoaddvol = new StereoAddVolCommand(stereo);
    		StereoSubVolCommand stereosubvol = new StereoSubVolCommand(stereo);
    
    		control.setCommand(0, bedroomlighton, bedroomlightoff);
    		control.setCommand(1, kitchlighton, kitchlightoff);
    		control.setCommand(2, stereoOn, stereoOff);
    		control.setCommand(3, stereoaddvol, stereosubvol);
    		control.setCommand(4, onmarco, offmarco);
    
    		control.onButton(0);
    		control.undoButton();
    		//control.offButton(0);
    		control.onButton(1);
    		control.offButton(1);
    		control.onButton(2);
    		control.onButton(3);
    				
    		control.offButton(3);
    		control.undoButton();
    		control.offButton(2);
    		control.undoButton();
    		control.onButton(4);
    		control.offButton(4);
    	}
    
    }
    
    
  • 相关阅读:
    转 new和malloc的区别
    转 内联函数
    转 C++宏定义详解
    转 内联函数和宏定义的区别
    转 C++中不能声明为虚函数的有哪些函数
    转 PV操作简单理解
    转 Python执行系统命令的方法
    转 常量指针和指针常量的区别详解
    转 python语法学习面向对象之继承
    Singleton、MultiThread、Lib——实现单实例无锁多线程安全API
  • 原文地址:https://www.cnblogs.com/kristse/p/command.html
Copyright © 2020-2023  润新知