Chapter 1 of design patterns-command patterns (JAVA implementation)

Source: Internet
Author: User

Chapter 1 of design patterns-command patterns (JAVA implementation)
Do you know about high cohesion? I don't know. (The author presses: In a certain mode, the force loading mode is being enabled, and fails to be enabled, and is rolled back .) So let the author give us a great deal of Science (I am bitter ~), The so-called high cohesion is a concept of software engineering, mainly object-oriented design, which refers to the relationship between classes, the so-called high cohesion means that each element in a module has a high degree of closeness. High Cohesion is a good fit for the design principles of a single responsibility. Now, the science popularization is over, and the food is gone. Well, what fish said is what I want to say, and I am a high cohesion model. Definition: 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 use different requests to parameterize the client, queue requests or record request logs, and provide command revocation and recovery functions. Using self-analysis in command mode, I can achieve the following results: decouple the object that calls an operation from the object that knows how to implement this operation. Multiple commands can be assembled into a composite command. It is easy to add new commands without changing the existing classes. Class decoupling. There is no dependency between the caller role and the receiver. It can be combined with my brother's responsibility chain mode to implement command family parsing tasks, and combined with the template method to reduce the problem of command subclass expansion. Disadvantages: if there are thousands of commands, there will be thousands of classes. The class expansion is too serious, but you can use the template method to reduce the class expansion. To implement the command mode, let's use the story of the teacher and James to implement the command mode. The first is the abstract class of the person who receives the command: 1 public abstract class extends er {2 // abstract Receiver, which defines the business required by the Receiver 3 public abstract void out (); 4 5} an abstract class is defined here, and an abstract method: exit. Then there is the implementation class of the specific receiver, which can be Xiaoqiang, Xiaodong, xiaoxi and so on. Here we will take Xiao Ming as an example: 1 public class XiaoMing extends er {2 // The command 3 public void out () {4 System. out. println ("James, get out. "); 5} 6} next is the abstract class of command: 1 public abstract class Command {2 // each command executes a method 3 public abstract void execute (); 4} is followed by the Implementation class of the specific Command: 1 public class ConcreteCommand extends Command {2 // execute the Command to process the corresponding worker er class 3 private worker er; 4 // The constructor transmits the Receiver 5 public ConcreteCommand (caller ER) {6 this. extends ER = extends er; 7} 8 9 // implement a command 10 public void execute () {11 this. aggreger. out (); 12} 13} is the final Invoker class Code implementation: 1 public class Invoker {2 private Command command; 3 // receive Command 4 5 public void setCommand (command Command) {6 this. command = command; 7} 8 9 // Execute command 10 public void action () {11 this.commadn.exe cute (); 12} 13} after calling, it is common: James, get out. Well, I feel something wrong. Command mode application scenarios can be implemented using Command mode in the following scenarios: different time periods, arrangement, and execution requests. Cancel the operation. Logs can be modified. Construct a system with high-level operations built on primitive operations. Above. Predict the form and listen to the next decomposition.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.