Design Mode command mode 6--

If hungry, eat, sleep sleepy, thirsty to drink, life is too boring


1 Command Mode

1.1 concept

Concept: The "request" encapsulated in objects, in order to use different requests, queue or log parameterized other objects. Command mode also supports undo operations.
By cited specific examples below to understand how the command mode now! !

1.2 references

To analyze customer examples point to the restaurant meal. Figure 1:

  1. Customers write down a hamburger, a bag of fries on the order. Call createOrder()a single point next meal.
  2. Waiter use takeOrder()method to fill customer orders away.
  3. Waiter call orderUp()notification chef prepares products.
  4. Chef get the order, looked content, calls makeHumburger()andmakeFries()
    Here Insert Picture Description

Code

//厨师类
public class Cook  {

	public Cook() {}

	public void makeBurger() {
		System.out.println("Making a burger");
	}

	public void makeFries() {
		System.out.println("Making fries");
	}

}

//顾客类
public class Customer {
    Waitress waitress;

    public Customer(Waitress waitress) {
        this.waitress = waitress;
    }

    public void hungry(Order o) {
        System.out.println("服务员点餐");
        order();
        waitress.takeOrder(o);
    }

    public void order() {
        System.out.println("我要汉堡和薯条");
    }
}

//服务员类
public class Waitress {
	Order order;

	public Waitress() {
	}

	public void takeOrder(Order order) {
		this.order = order;
		order.orderUp();
	}
}

//订单接口
public interface Order {
	public void orderUp();
}

//测试代码
public class Diner {
	public static void main(String[] args) {
		Cook cook = new Cook();
		Waitress waitress = new Waitress();
		Customer customer = new Customer(waitress);

		Order o = () -> {
			cook.makeBurger();
			cook.makeFries();
		};
		customer.hungry(o);
	}
}

1.3 Analysis

Generated from order to end the order, the waiter does not need to know the actual contents of the order, the waiter's job is to cook to order from the hands of the hands of customers. This example will request author (customer) and the implementation of the requester (the chef) apart. This is the command mode ideas.
Figure 2:

  1. Client (client) to create a command object (Command), a set of actions receivers (Receiver) to do the command object contains.

  2. Command object provides a execute()method of encapsulating the recipient needs to do the operation. Call these methods will call these actions of the recipient.

  3. Client calls the caller (Invoker) of setCommand(), and the command object passed to the method.

  4. The caller calls the command object's execute()methods.

  5. The final recipient of the implementation of these actions

Here Insert Picture Description

1.4 Command Mode FIG class

  • The Command is defined as an interface, call the execute()related method of operation will make the recipient.
  • InvokerIn holding a command object and invoke the command object point at a certain time execute(), to achieve the request.
  • Responsible for creating a client ConcreteCommand, and set its recipient.

Here Insert Picture Description)

Example 1.5 Improved primer

(1) in FIG.

  • In the Cook makeBurger()and makeFries()abstracted into Commandforming two implement Commandinterface classes: CookMakeBurgerCommandand CookMakeFriesCommand.
  • The waitressabstracted into Invoker. By takeOrder()calling the method Commandinterface execute()methods.

Here Insert Picture Description
(2) Code

public interface Order {
	public void execute();
}


public class CookMakeFriesCommand implements Order {
    private Cook cook;

    public CookMakeFriesCommand(Cook cook) {
        this.cook = cook;
    }

    @Override
    public void execute() {
        cook.makeFries();
    }
}

public class CookMakeBurgerCommand implements Order {
    private Cook cook;

    public CookMakeBurgerCommand(Cook cook) {
        this.cook = cook;
    }

    @Override
    public void execute() {
        cook.makeBurger();
    }
}

public class Cook  {

	public Cook() {}

	public void makeBurger() {
		System.out.println("Making a burger");
	}

	public void makeFries() {
		System.out.println("Making fries");
	}

}

public class Customer {
    Waitress waitress;

    public Customer(Waitress waitress) {
        this.waitress = waitress;
    }

    public void hungry(Order o) {
        System.out.println("服务员点餐");
        waitress.takeOrder(o);
    }

}

public class Waitress {
	Order order;

	public Waitress() {
	}

	private void setOrder(Order order){
		this.order=order;
	}

	public void takeOrder(Order order) {
		setOrder(order);
		order.execute();
	}
}

public class Diner {
	public static void main(String[] args) {
		Cook cook = new Cook();
		Waitress waitress = new Waitress();
		Customer customer = new Customer(waitress);
		CookMakeFriesCommand friesCommand=new CookMakeFriesCommand(cook);
		CookMakeBurgerCommand burgerCommand=new CookMakeBurgerCommand(cook);

		customer.hungry(friesCommand);
		customer.hungry(burgerCommand);


	}
}

Example 2. Exercise (remote control)

API needs to be designed for the smart home remote control to control all kinds of smart home. First with lights and garage door design example.

FIG class 2.1

Through this example you can see, when a large object method generates a corresponding command class also more up, this is the disadvantage of command mode.

Here Insert Picture Description

2.2 Code

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

public class Light {
    public Light() {
    }

    public void on() {
        System.out.println("Light is on");
    }

    public void off() {
        System.out.println("Light is off");
    }
}

public interface Command {
    void execute();
}

public class GarageDoorDownCommand implements Command {
    GarageDoor garageDoor;

    public GarageDoorDownCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.down();
    }

}

public class GarageDoorLightOffCommand implements Command {
    GarageDoor garageDoor;

    public GarageDoorLightOffCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.lightOff();
    }
}

public class GarageDoorOpenCommand implements Command {
    GarageDoor garageDoor;

    public GarageDoorOpenCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    public void execute() {
        garageDoor.up();
    }
}

public class GarageDoorStopCommand implements Command {
    GarageDoor garageDoor;

    public GarageDoorStopCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.stop();
    }
}

public class GarageLightOnCommand implements Command {
    GarageDoor garageDoor;

    public GarageLightOnCommand(GarageDoor garageDoor) {
        this.garageDoor = garageDoor;
    }

    @Override
    public void execute() {
        garageDoor.lightOn();
    }
}

public class LightOffCommand implements Command {
    Light light;

    public LightOffCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.off();
    }
}

public class LightOnCommand implements Command {
    Light light;

    public LightOnCommand(Light light) {
        this.light = light;
    }

    @Override
    public void execute() {
        light.on();
    }
}

public class SimpleRemoteControl {
    Command slot;

    public SimpleRemoteControl() {}

    public void setCommand(Command command) {
        slot = command;
    }

    public void buttonWasPressed() {
        slot.execute();
    }
}


public class RemoteControlTest {
    public static void main(String[] args) {
        SimpleRemoteControl remote = new SimpleRemoteControl();
        Light light = new Light();
        GarageDoor garageDoor = new GarageDoor();
        LightOnCommand lightOn = new LightOnCommand(light);
        GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);
        GarageDoorDownCommand garageDoorDownCommand = new GarageDoorDownCommand(garageDoor);
        GarageDoorStopCommand garageDoorStopCommand = new GarageDoorStopCommand(garageDoor);
        GarageDoorLightOffCommand garageDoorLightOffCommand = new GarageDoorLightOffCommand(garageDoor);
        GarageLightOnCommand garageLightOnCommand = new GarageLightOnCommand(garageDoor);

        remote.setCommand(garageOpen);
        remote.buttonWasPressed();
        remote.setCommand(garageLightOnCommand);
        remote.buttonWasPressed();
        remote.setCommand(garageDoorStopCommand);
        remote.buttonWasPressed();
        remote.setCommand(garageDoorDownCommand);
        remote.buttonWasPressed();
        remote.setCommand(garageDoorLightOffCommand);
        remote.buttonWasPressed();
        remote.setCommand(lightOn);
        remote.buttonWasPressed();
    }
}

Published 16 original articles · won praise 3 · Views 954

Guess you like

Origin blog.csdn.net/weixin_41938180/article/details/104334546