preface
Today, in the process of interviewing others, I asked about the design mode. He said command mode, what!! I haven't heard of it. I can only pretend to be calm, ask him, and quickly turn over the book when I come back.
Command mode
Daily endorsement: Command Pattern is a data-driven design pattern, which belongs to behavioral pattern. The request is wrapped in the object in the form of command and passed to the calling object. The calling object looks for the appropriate object that can handle the command, passes the command to the corresponding object, and the object executes the command. Tell a story first, and then talk about your understanding.
Board story
I'm sure everyone has eaten the noodles. Today we're talking about a noodle shop on the other side of the company. Our Pipi family often go to eat. There are only the boss and the landlady in this shop. They can't remember what we ordered and the order of ordering every time. The common scene is that the old board holds a bowl of noodles and says that brother's noodles and intestines are good. You say with black lines on your face, I ordered GaiFan. Let's use code to describe the business model of this board.
Plate surface 1.0: tight coupling
Noodle 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 intestines and eggs!"); } public void makeRice(){ System.out.println("Brother, your meal!"); } }
The Pipi family came to the noodle 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 came to the noodle shop and ordered three noodles and two rice noodler.makeNoodle(); noodler.makeRice(); noodler.makeNoodle(); noodler.makeRice(); noodler.makeNoodle(); } }
effect:
Board 1.0 describes the business model of the current board store. The implementation is very simple, but it has exposed many problems.
In terms of real business
As consumers, we interact directly with Chefs (cooking is also done by the boss and the boss's wife). They can't focus on their essential work (cooking). They need to remember what everyone ordered and the order of each meal, which leads to the current situation. It depends not on what you ordered, but on what they did. The things you ordered may not be able to be made without raw materials, But they can't inform you in time. If you want to change a meal, he may not be able to take into account your needs. This kind of non single responsibility makes them unable to take into account both cooking and greeting guests, and the work on both sides may not be done well
In terms of code
In this design, we act as the behavior requester and the board shop acts as the behavior executor. The two are tightly coupled. For some simple scenarios, this is more appropriate. The requester interacts directly with the executor. However, in some occasions, such as when it is necessary to record, revoke or redo the behavior, and deal with transactions, this tightly coupled design is not very appropriate
Board surface 2.0: use command mode to decouple the board surface
Our Pipi family has been discussing the problem of the board noodle shop. We think the key point is to separate greeting guests from cooking. In our code, we modify the responsibilities of the boss and the landlady so that they are 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'm only responsible for cooking! I'm" + s); } //The boss and the landlady check the kitchen information to see if the guests can do what they ordered public boolean getInfo(String s){ if ("Board surface".equals(s)){ return true; }else if ("GaiFan".equals(s)){ return true; }else if ("Leek dumplings".equals(s)){ System.out.println("There are no leeks. Don't let the guests order leek dumplings!"); return false; }else { return true; } } }
Make an order system for the board shop:
package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public interface Command { /** * The order system informs the boss and his wife to cook */ void execute(); /** * Get kitchen information * @return */ String getMakeInfo(); }
In the order system, add the information of a board surface
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("The order system informs the boss to make a bowl of noodles!"); maker.make("Make board surface"); } @Override public String getMakeInfo() { if (maker.getInfo("Board surface")){ System.out.println("Boss, check the kitchen information!"); System.out.println("If you find materials, you can make board surface!"); System.out.println("Through the order system, inform the waiter to greet the guests and say: you can order board noodles!"); return "Boss, make me a bowl of noodles!"; }else { System.out.println("Without materials, you can't make board surface!"); return "NULL"; } } }
Recruit a waiter for the board noodle shop, who is specially responsible for greeting guests:
package edu.design.pattern.command; import java.util.ArrayList; import java.util.List; /** * Waiter class * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class Waiter { //Attendant control order system private List<Command> commandList = new ArrayList<>(); //The waiter greets the guest and enters the guest's ordering information into the ordering system //There are no leeks today. I can't order leek dumplings public void setCommand(Command command){ if (command.getMakeInfo().equals("NULL")){ System.out.println("There are no leeks. I can't make leek dumplings!"); }else { commandList.add(command); } } //The waiter informs the boss and the boss's wife to cook in the order system public void notifyMaker(){ if (commandList.size() == 0){ System.out.println("There are no guests to order for the time being!"); } for (Command command : commandList){ command.execute(); } } }
When the noodle shop reopened, the Pipi family came again:
package edu.design.pattern.command; /** * @author ZhaoWeinan * @date 2018/3/22 * @description */ public class CommandDemo { public static void main(String[] args){ //Boss on stage Maker maker = new Maker(); //The waiter appeared and greeted the guests Waiter waiter = new Waiter(); //The waiter checks the notice of the order system and checks whether the board can be ordered Command command = new NoodleCommand(maker); //The waiter ordered a plate through the order system waiter.setCommand(command); //The waiter clicks confirm and the order system informs the boss to cook waiter.notifyMaker(); } }
effect:
Through the upgrade of board 2.0, these problems in 1.0 have been solved. After telling the story of board, let's summarize it.
summary
Let's summarize from the above story
Problems to be solved
The design of tight coupling between the behavior requester and the executor is more appropriate for some simple scenarios. The requester interacts directly with the executor. However, in some occasions, such as when the behavior needs to be recorded, revoked or redone, transaction and other processing, this kind of tight coupling design is not very appropriate.
Solution
The command mode defines three main roles: the command execution object receiver, the command object command, and the entry invoker of the command request. Through the entry of the command request, the requester passes the command to the receiver for the execution of the command, so as to understand the process of request → execution
advantage
1. Reduced system coupling
2. It is easier to log commands
3. Allows the command receiver to decide whether to accept the command
4. New commands can be easily added to the system
Command mode is here for you. Welcome to communicate and point out some mistakes in the text, so that I can deepen my understanding.
Thank you!
last:
Recently, I compiled a complete set of "summary of Java core knowledge points". To be honest, as a java programmer, you should take a good look at this material whether you need an interview or not. Kwai won't lose my hand. Many of my fans got Offer from Tencent's byte express company.
Click in Java Architecture Resource Exchange Group , find the administrator to get Oh -!