When I was interviewing someone today, I asked him about design mode, and he said command mode, what!! I had never heard of it, so I had to play it cool and ask him about it. When I got back, I had to read my book and fix it.

Command mode

Daily endorsement: Command Pattern is a data-driven design Pattern, which belongs to behavior Pattern. The request is wrapped in the object as a command and passed to the calling object. The calling object looks for a suitable object that can handle the command and passes the command to the corresponding object, which executes the command. Let’s tell a story first, and then talk about their own understanding.

The story of the board

Panel believe everyone ate right, today is a company on the other side of the panel shop, pipi family often go to, we eat in the store only the boss and the wife of shop-owner, always can’t remember what we ordered, also can’t remember the order of the order, frequent scenario is that the boss with a bowl of panel said brother panel plus im ok, you with black line, says my order is over rice. Let’s use code to describe the business model of this board.

Board 1.0: Tightly coupled

Panel shop:

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/21 * @description */ public class Noodler { public void makeNoodle(){ System.out.println(" brother, your noodles and sausage and egg!" ); } public void makeRice(){system.out.println (" ); }}Copy the code

Pipi family came to the board shop

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/21 * @description */ public class Demo { public static void main(String[] args){  Noodler noodler = new Noodler(); // The Pipi family went to the restaurant where noodles made of three noodles and two covered with rice noodler.makeNoodle(); noodler.makeRice(); noodler.makeNoodle(); noodler.makeRice(); noodler.makeNoodle(); }}Copy the code

Effect:

Board 1.0, describes the current board shop business model, the implementation is very simple, but exposed a lot of problems.

From the real business point of view

We as consumers directly to cook (cooking) is the boss and the wife of shop-owner do interact, they can’t focus on the nature of their work (cooking), what need to remember that everyone order, the order of each order, creates the situation, not what you ordered, but look at what they do, you may have no raw materials can’t do something, However, they may not be able to timely inform you that if you want to change your meal, they may not be able to take into account your needs. Due to their different responsibilities, they may not be able to take into account the cooking and ordering of guests at the same time, so they may not be able to do well on both sides of the work

Look at the code

This design, we request as behaviors, and panel shop as a behavior, the two are tightly coupled, for some simple scenario, do more appropriate, requestor directly interact with practitioners, but on some occasions, such as the need to record the behavior, undo or redo, transaction processing, such as the tight coupling of the design is not very appropriate

Board 2.0: Decouple board stores using command mode

We in the Pipi family have been discussing the problems of the noodle shop and think that the key point is to separate greeting from cooking. In our code, we modify the responsibilities of the owner and the owner’s wife to make them only responsible for cooking:

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class Maker { public void make(String s){ System.out.println(" Brother, I only cook! I'm working on it. } public Boolean getInfo(String s){if (" equals "(s)){return true; }else if (" equals ". Equals (s)){return true; }else if (" chive dumplings ".equals(s)){system.out.println (" chive dumplings ".equals(s)){system.out.println (" chive dumplings ".equals(s)); ); return false; }else { return true; }}}Copy the code

Create an order system for the board shop:

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public interface Command {/** ** execute(); /** * get kitchen info * @return */ String getMakeInfo(); }Copy the code

In the order system, add information for a board

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class NoodleCommand implements Command { private Maker maker; public NoodleCommand(Maker maker) { this.maker = maker; } @override public void execute() {system.out.println (" ); Maker. Make (" make "); } @override public String getMakeInfo() {if (maker.getInfo()){system.out.println (); ); System.out.println(" found some material, can do the board!" ); System.out.println(" Through the order System, notify the waiter to greet the guests say: can point the board!" ); Return "Boss, make a bowl of noodles!" ; }else {system.out.println (" no material, can't do the board! ") ); return "NULL"; }}}Copy the code

A waiter is wanted for the board shop, who is responsible for greeting customers:

package edu.design.pattern.command; import java.util.ArrayList; import java.util.List; /** ** server class * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class Waiter List<Command> commandList = new ArrayList<>(); // The waiter greets the guests and enters their order information into the ordering system // There are no leeks today, Public void setCommand(Command Command){if (command.getmakeinfo ().equals("NULL")){system.out.println (" I can't make leek dumplings!" ); }else { commandList.add(command); Public void notifyMaker(){if (commandlist.size () == 0){system.out.println (" There is no guest ordering at the moment!" ); } for (Command command : commandList){ command.execute(); }}}Copy the code

The board shop reopened, and the Pippi family returned:

package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class CommandDemo { public static void main(String[] Args){// Maker Maker = new Maker(); // new Waiter = new Waiter(); Command = new NoodleCommand(maker); Command = new NoodleCommand(maker); // the waiter uses the order system. setCommand(command); // The order system notifies the boss to cook the waiter. NotifyMaker (); }}Copy the code

Effect:

With this board 2.0 update, these problems in 1.0 have been solved. The board story has been told. Let’s summarize it.

conclusion

Let’s sum up from the above story

Problems that need to be solved

A tightly coupled design between the behavior requester and the performer may be appropriate for simple scenarios where the requester interacts directly with the performer, but may not be appropriate for situations where the behavior needs to be recorded, undone or redone, or transacted.

Solution

The command mode has three main roles: command execution object receiver, command object command and invoker, the entrance of command request. The requester passes the command to the receiver for command execution through the entrance of command request, so as to decouplex the process from request to execution

advantages

3. Allow the receiver of the command to decide whether or not to accept the command. 4

Command mode for everyone here, welcome everyone to exchange, point out some of the wrong places in the article, let me deepen my understanding.

Thank you!