In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The article begins with:
Behavioral mode: command mode
The third of the eleven behavioral models: command mode.
Brief introduction
Name: command mode
English name: Command Pattern
Values: a mountain of orders
Personal introduction:
Encapsulate a request as an object,thereby letting you parameterize clients with different requests,queue or log requests,and support undoable operations.
Encapsulate a request into an object so that you can parameterize the client with different requests, queue requests or log requests, and provide command revocation and recovery functions.
(from the Zen of Design patterns)
The story you want.
As programmers, we go through the command mode every day, in which the technical manager assigns the requirements task to the engineer, sometimes stopping the development of the requirements because of a third party or other irresistible factors. This working mode is the command mode. All right, let's start the story. Xiao Ming worked as a quiet programmer at XX Technology, and one day the technical manager assigned him a task: add a blacklist, that is, the ability to manually blacklist calls in a module of their system. Xiaoming developed immediately after receiving the task. after 2 days of development, the technical manager Daming suspended the development task for strategic reasons, and then we implemented this process through two kinds of code implementation: non-command mode and command mode. In this scenario, for simplicity, we assume that Daming, the technical manager, has only one developer under Xiaoming.
Non-command mode
Non-command mode is a code implementation that does not use command mode. In the code, we have a Developer developer, the developer classmate accepts the task conveyed by the technical manager, and the technical manager asks him to develop which requirement. If there is a problem in the project that needs to be interrupted, it also needs to be communicated to the developer classmate after evaluation by the technical manager, so Developer has two methods, namely develop () development requirements and suspend () suspension requirements. Requirement is the requirements class, and TechnicalManager1 is the technical manager class. He has a method action (), which is used to specify the development task or pause the task.
Public class NoCommandTest {public static void main (String [] args) {Developer xiaoMing = new Developer ("Xiaoming"); Requirement requirement = new Requirement ("new blacklist"); TechnicalManager1 technicalManager2 = new TechnicalManager1 ("Daming"); technicalManager2.setDeveloper (xiaoMing); technicalManager2.action (requirement, "develop"); System.out.println ("2 days of development, demand changes, need to be suspended.") ; technicalManager2.action (requirement, "suspend");}} / * * developer * / class Developer {private String name; public Developer (String name) {this.name = name;} public void develop (Requirement requirement) {System.out.println (this.name + "start development requirements:" + requirement.getName ()) } public void suspend (Requirement requirement) {System.out.println (this.name + "stop development requirements:" + requirement.getName ());} public String getName () {return name;}} / * requirements * / class Requirement {private String name; public Requirement (String name) {this.name = name;} public String getName () {return name }} / * Technical Manager * / class TechnicalManager1 {private String name; private Developer developer; public TechnicalManager1 (String name) {this.name = name;} public void setDeveloper (Developer developer) {this.developer = developer;} public void action (Requirement requirement, String type) {if ("develop" .equals (type)) {this.developer.develop (requirement) } else if ("suspend" .equals (type)) {this.developer.suspend (requirement);} print result: Xiaoming begins to develop requirements: the new blacklist has been developed for 2 days, and the demand changes and needs to be suspended. Xiaoming stops developing requirements: add blacklist
Through the code, we can find that there is a strong dependency between the technical manager and the developer. If the technical manager gives a task and asks Xiaoming to write a weekly report, what should be written at this time? Does Xiaoming need a way to write weekly reports, and Daming also needs to add a transaction type? Is there a better way for the technical manager not to make any changes? Command mode is used to solve this problem.
Command mode
In this example, no matter what Daming asks Xiaoming to do, it is actually the same, that is, to issue a task order and let Xiaoming carry out the order. We can use the command mode to abstract the task as a parent class, issuing development commands, pausing orders, writing weekly reports, and so on are all different sub-commands. The code is as follows.
Public class CommandTest {public static void main (String [] args) {Developer xiaoMing = new Developer ("Xiaoming"); Command developCommand = new DevelopCommand (xiaoMing); Command suspendCommand = new SuspendCommand (xiaoMing); Requirement requirement = new Requirement ("new blacklist"); TechnicalManager2 technicalManager = new TechnicalManager2 ("Daming"); technicalManager.setCommand (developCommand); technicalManager.action (requirement) System.out.println ("after 2 days of development, the demand changes and needs to be paused.") ; technicalManager.setCommand (suspendCommand); technicalManager.action (requirement);}} / * * Command * / abstract class Command {protected Developer developer; public Command (Developer developer) {this.developer = developer;} public abstract void execute (Requirement requirement);} / * start development * / class DevelopCommand extends Command {public DevelopCommand (Developer developer) {super (developer) } @ Override public void execute (Requirement requirement) {this.developer.develop (requirement);}} / * Development interrupt * / class SuspendCommand extends Command {public SuspendCommand (Developer developer) {super (developer);} @ Override public void execute (Requirement requirement) {this.developer.suspend (requirement);}} / * Technical Manager * / class TechnicalManager2 {private String name Private Command command; public TechnicalManager2 (String name) {this.name = name;} public void action (Requirement requirement) {this.command.execute (requirement);} public void setCommand (Command command) {this.command = command;}} print result: Xiaoming begins to develop requirements: the new blacklist has been developed for 2 days, and the demand changes and needs to be suspended. Xiaoming stops developing requirements: add blacklist
Command is used to abstract the assigned tasks in the code, and the technical manager TechnicalManager2 does not have a direct relationship with Developer, but the relationship between TechnicalManager2 and Command, Command and Developer. In this way, the strong dependency relationship between Daming and Xiaoming is stripped away, and it is also very simple to add a new task to write a weekly report. Add a method to write a weekly report in Developer, add a Command subcategory to write a weekly report, and TechnicalManager2 does not need to be modified as above. This is the complete command mode code.
Code:
Command Pattern
Summary
From the article, we can see that the command mode can be used to decouple the class, so that there is no relationship between the caller and the receiver, and through the abstraction of the behavior, it becomes clear and easy to add other behaviors, that is, the scalability is greatly increased.
Recommended reading:
Creative mode: singleton mode (Xiaoming has only one car)
Creative model: factory method (Xiaoming's garage)
Creative model: abstract factory (BMW cars have to use BMW tires and BMW steering wheels)
Creative model: builder mode (soup is so hot)
Creative model: prototype model (photocopying books)
Behavioral model: template method (sneaker manufacturing process)
Behavioral model: intermediary model (renting a house to find an intermediary)
The official account replied "Gift package" to get tutorials such as Java, Python, IOS, etc.
Add personal Wechat comments "tutorials" to get tutorials for architects, machine learning, etc.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.