• 设计模式


    命令模式(command pattern) 详细解释


    本文地址: http://blog.csdn.net/caroline_wendy


    命令模式(command pattern) : 将请求封装成对象, 以便使用不同的请求队列日志来參数化其它对象.

    命令模式也能够支持撤销操作.


    简单的命令模式的实现:

    1. 详细的类, 每个类都有特定的方法:

    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class Light {
    
    	public Light() {}
    	
    	public void on() {
    		System.out.println("Light is on");
    	}
    	
    	public void off() {
    		System.out.println("Light is off");
    	}
    	
    }
    
    
    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class GarageDoor {
    	
    	public GarageDoor() {}
    
    	public void up() {
    		System.out.println("Garage Door is Open");
    	}
    
    	public void down() {
    		System.out.println("Garage Door is Closed");
    	}
    
    	public void stop() {
    		System.out.println("Garage Door is Stopped");
    	}
    
    	public void lightOn() {
    		System.out.println("Garage light is on");
    	}
    
    	public void lightOff() {
    		System.out.println("Garage light is off");
    	}
    }
    



    2. 命令接口, 提供全部命令的接口, 详细命令继承接口, 并实现方法:

    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public interface Command {
    	public void execute();
    }
    

    2. 详细的命令实现, 即把详细类的函数, 封装入命令接口:

    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class LightOnCommand implements Command {
    
    	Light light;
    	
    	public LightOnCommand (Light light) {
    		this.light = light;
    	}
    	
    	/* (non-Javadoc)
    	 * @see command.Command#execute()
    	 */
    	@Override
    	public void execute() {
    		// TODO Auto-generated method stub
    		light.on();
    	}
    
    }
    
    
    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class GarageDoorOpenCommand implements Command {
    	
    	GarageDoor garageDoor;
    	
    	public GarageDoorOpenCommand (GarageDoor garageDoor) {
    		this.garageDoor = garageDoor;
    	}
    	
    	/* (non-Javadoc)
    	 * @see command.Command#execute()
    	 */
    	@Override
    	public void execute() {
    		// TODO Auto-generated method stub
    		garageDoor.up();
    	}
    
    }
    

    3. 接受者, 运行请求, 传入命令參数, 统一运行命令:

    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class SimpleRemoteControl {
    	
    	Command slot;
    	
    	public SimpleRemoteControl() {}
    	
    	public void setCommand(Command command) {
    		this.slot = command;
    	}
    	
    	public void buttonWasPressed() {
    		slot.execute();
    	}
    }
    

    4. 运行类, 新建详细类, 新建详细命令, 把详细命令传入接收者类, 运行统一动作:

    /**
     * @time 2014年6月9日
     */
    package command;
    
    /**
     * @author C.L.Wang
     *
     */
    public class RemoteControlTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		SimpleRemoteControl remote = new SimpleRemoteControl();
    		Light light = new Light();
    		GarageDoor garageDoor = new GarageDoor();
    		LightOnCommand lightOn = new LightOnCommand(light);
    		GarageDoorOpenCommand garageOpen = 
    				new GarageDoorOpenCommand(garageDoor);
    		
    		remote.setCommand(lightOn);
    		remote.buttonWasPressed();
    		remote.setCommand(garageOpen);
    		remote.buttonWasPressed();
    		
    	}
    
    }
    

    5. 输出:

    Light is on
    Garage Door is Open
    







  • 相关阅读:
    [LeetCode] Duplicate Emails 重复的邮箱
    [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
    [CareerCup] 15.4 Types of Join 各种交
    [LeetCode] Employees Earning More Than Their Managers 员工挣得比经理多
    [LeetCode] Consecutive Numbers 连续的数字
    [LeetCode] Rank Scores 分数排行
    [CareerCup] 15.3 Renting Apartment III 租房之三
    [CareerCup] 15.2 Renting Apartment II 租房之二
    [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
    [CareerCup] 15.1 Renting Apartment 租房
  • 原文地址:https://www.cnblogs.com/hrhguanli/p/3837947.html
Copyright © 2020-2023  润新知