1. Definitions
Command mode is a behavioral mode, which encapsulates a request as an object, so that you can parameterize customers with different requests, queue or log requests, and support revocable operations.
As developers, they often receive requirements and then code according to the requirements. Of course, the requirements are generally based on the prototype of the product, UI design, front-end and back-end development, and finally tested through testing. If the demand changes on this day, it is the so-called order. Let the UI change to the following figure. Or let developers add a new feature. Then there is the following operation.
Group is an abstraction of different people. Their subclasses include product managers, ui and developers.
public abstract class Group { //If Party A and Party B work separately, you should discuss with that group. You should first find this group public abstract void find(); //Required to add functions public abstract void add(); //Requested to delete function public abstract void delete(); //Required to modify function public abstract void change(); //All change plans are required public abstract void plan(); }
ProductManagerGroup, UiGroup and CodeGroup represent product group, UI group and development group respectively
public class ProductManagerGroup extends Group{ //The customer asked the requirements group to go over and talk to them @Override public void find() { System.out.println("Find requirement group..."); } //The customer asked for an additional demand @Override public void add() { System.out.println("The customer asked for an additional demand..."); } @Override //The customer requests to modify a requirement public void change() { System.out.println("The customer requests to modify a requirement..."); } //The customer asked to delete a requirement @Override public void delete() { System.out.println("The customer asked to delete a requirement..."); } //Change plan required by customer @Override public void plan() { System.out.println("Customer requirements change plan..."); } }
public class UiGroup extends Group{ //The customer asked the requirements group to go over and talk to them @Override public void find() { System.out.println("Find the art team..."); } //The customer asked for an additional demand @Override public void add() { System.out.println("The customer asked for an additional demand..."); } @Override //The customer requests to modify a requirement public void change() { System.out.println("The customer requests to modify a requirement..."); } //The customer asked to delete a requirement @Override public void delete() { System.out.println("The customer asked to delete a requirement..."); } //Change plan required by customer @Override public void plan() { System.out.println("Customer requirements change plan..."); } }
public class CodeGroup extends Group{ //The customer asked the requirements group to go over and talk to them @Override public void find() { System.out.println("Find development group..."); } //The customer asked for an additional demand @Override public void add() { System.out.println("The customer asked for an additional demand..."); } @Override //The customer requests to modify a requirement public void change() { System.out.println("The customer requests to modify a requirement..."); } //The customer asked to delete a requirement @Override public void delete() { System.out.println("The customer asked to delete a requirement..."); } //Change plan required by customer @Override public void plan() { System.out.println("Customer requirements change plan..."); } }
At this time, if you want to find a product to change the requirements, you can find the product first, then add functions, and finally change the requirements.
public class Test { public static void main(String[] args) { Group productManagerGroup = new ProductManagerGroup(); productManagerGroup.find(); productManagerGroup.add(); productManagerGroup.plan(); } }
Find requirement group... The customer asked for an additional demand... Customer requirements change plan...
If you need to find developers to add functions, you also need to find developers first, then add functions, and finally change requirements.
public class Test { public static void main(String[] args) { Group codeGroup = new CodeGroup(); codeGroup.find(); codeGroup.add(); codeGroup.plan(); } }
Find development group... The customer asked for an additional demand... Customer requirements change plan...
Now, if you want to find the UI change diagram, do you need to find the UI group first and then carry out the corresponding operation, but the requirements change all the time? If the product manager changes the corresponding requirements and the developers don't know, is it possible that the final actual effect is inconsistent with what the customer wants, because the product is not synchronized with the development.
And for customers, should all groups be notified every time I change the requirements? Can you give me a docking person to change my requirements, that is, commands, just tell the person who synchronizes with me, and then the person who synchronizes tells all developers. If a middleman is added in the middle, the customer and the corresponding developer can understand each other.
2. Command mode implementation
Based on the requirements of developers, products and artists, we add an intermediary, otherwise the customer will always ask someone to change it alone, and the change will not be synchronized.
Command is a command. The commands proposed by the customer must be executed by someone. Therefore, the product group, art group and code group are referenced in the command. After all, when the command comes, you need to know who will do it, and execute is the abstraction of doing things. As for who will do it and how to do it, the subclass should decide
public abstract class Command { //All three groups are defined, and subclasses can be used directly //Product group protected ProductManagerGroup pmg = new ProductManagerGroup(); //Art Group protected UiGroup ug = new UiGroup(); //Code group; protected CodeGroup cg = new CodeGroup(); //Just one way, what do you want me to do public abstract void execute(); }
Invoker is the middleman. The customer only needs to tell the middleman what I want to change. As for who to change, it is the middleman who decides. If the prototype drawing, ui design drawing and front-end and back-end code of the product need to be changed, it is also the middleman who decides.
public class Invoker { private Command command; public Invoker(Command command) { this.command = command; } //Execute customer's orders public void action(){ this.command.execute(); } }
AddRequirementCommand, ChangeRequirementCommand and DeleteRequirementCommand are commands for changing requirements. They decide who to do. I can change products, art and development, or only one group. Now, if you want to change a requirement, can you notify the corresponding group, and the caller and the developer understand each other.
public class AddRequirementCommand extends Command{ @Override public void execute() { super.pmg.find(); super.pmg.add(); super.pmg.plan(); } }
public class ChangeRequirementCommand extends Command{ @Override public void execute() { super.pmg.find(); super.pmg.change(); super.pmg.plan(); } }
public class DeleteRequirementCommand extends Command{ @Override public void execute() { super.pmg.find(); super.pmg.delete(); super.pmg.plan(); } }
Now, for customers, they only need to find middlemen. They no longer need to go to the group one by one. At the same time, they also avoid groups that may not be notified.
public class Test { public static void main(String[] args) { Invoker invoker = new Invoker(new AddRequirementCommand()); invoker.action(); } }
Find requirement group... The customer asked for an additional demand... Customer requirements change plan...
Roles in command mode
Receiver role: This is the role of working. Commands passed here should be executed. Specifically, in our example above, there are three implementation classes of Group;
Command role: it is a command. All commands I need to execute are declared here; In the example, the corresponding AddRequirementCommand, ChangeRequirementCommand, and DeleteRequirementCommand.
Invoker role: the caller receives the command and executes it. In this example, it is the Invker class.
Reference: Zen of design pattern
Code acquisition address: https://gitee.com/bughong/design-pattern