The 14th chapter of the design pattern-command mode (Java implementation)
"Xiaoming, get the hell out." "Xiaoming, how does this matter?" "(poor xiaoming was helpless to lay a gun.) Xiao Ming: Teacher, I and you have what grudge, I and you have what grudge what resentment ah 、、、 teacher: Xiao Ming, get out. Get used to it. Xiao Ming: 、、、) for this phenomenon, please ask the command mode to do a commentary.
Self introduction of command mode
You know high cohesion? I don't know, but I don't know. (Author Press: A mode to open the mode of loading, loading mode is open 、、、 open failed, rollback. So let the author give us a lot of science (poor I ah ~), the so-called high cohesion is a concept of software engineering, mainly object-oriented design, that is, the relationship between class and class, so-called high cohesion means that a module in the integration of the various elements of a higher degree of tightness. High cohesion is a good fit for a single responsibility design principle. Well, the science is over and foraging has gone. NN, fish brother said is what I want to say, and I am a high-cohesion mode. Definitions are: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log req Uests, and support undoable operations. A request is encapsulated into an object, allowing you to parameterize the client with different requests, queue requests, or log request logs, which can provide undo and redo functions for the command. The general class diagram is as follows:
It's so clear on the picture that I'm finished with what I want to say.
Self-analysis of the command pattern
The following effects can be achieved with me:
- Decouple the object that invokes the operation from the object that knows how to implement the operation.
- Multiple commands can be assembled into a composite command.
- It's easy to add new commands without changing the existing classes.
- Decoupling between classes, there is no dependency between the caller role and the receiver.
- Can be combined with my brother's responsibility chain mode, implement the command Family resolution task, and the template method, can reduce the problem of command sub-class bloat.
Insufficient:
- If there are thousands of commands, you need to have thousands of classes, the class bloat is too serious, but you can combine the template method to reduce the expansion of the class.
Implementation of command mode
Then we take the teacher and Xiao Ming's story to achieve the command mode, the first is naturally the person receives the command abstract class:
1 Public Abstract class receiver{2 // an abstract recipient who defines the business that the recipient needs to complete 3 Public Abstract void out (); 4 5 }
This defines an abstract class, and an abstract method: go out.
then is the specific receiver of the implementation class, can be small walls, small east, Konishi and so on, here take Xiao Ming to raise chestnuts:
1 Public class extends receiver{2 // commands executed by the recipient 3 Public void out () {4 System.out.println ("Xiaoming, Get out.") "); 5 }6 }
Next is the command's abstract class:
1 Public Abstract class command{2 // each command executes a method 3 Public Abstract void execute (); 4 }
The following is the implementation class for the specific command:
1 Public classConcretecommandextendscommand{2 //command processing for the corresponding receiver class3 Privatereceiver receiver;4 //Constructor Pass -through recipients5 PublicConcretecommand (receiver receiver) {6 This. Receiver =receiver;7 }8 9 //Implement a commandTen Public voidExecute () { One This. Receiver.out (); A } -}View Code
Finally, the code implementation of the Invoker class:
1 Public classinvoker{2 Privatecommand command;3 //receive Command4 5 Public voidsetcommand (Command command) {6 This. Command =command;7 }8 9 //Execute CommandTen Public voidaction () { One This. Commadn.execute (); A } -}View Code
by after the call, it is common for us: xiaoming, get out. Well, it feels wrong.
Application Scenarios of command mode
In the following scenario, you can use the command mode to:
- Different moments, permutations, and execution requests.
- Support Cancel operation.
- Support for modifying logs.
- Construct a system with high-level operations built on primitive operations.
Above. How to predict the post-style, and listen to tell.
PS: This blog welcome forwarding, but please specify the blog address and author ~
Blog Address: http://www.cnblogs.com/voidy/
Blog: http://voidy.gitcafe.com
<. ))) ≦
The 14th chapter of the design pattern-command-line mode (Java implementation)