Commands in Java design mode and in java Design Mode

Source: Internet
Author: User

Commands in Java design mode and in java Design Mode

This article continues to introduce the command modes of the 23 design mode series.

Define to pass requests from the client into an object so that you can parameterize the customer with different requests. This function is used to decouple "behavior requestor" from "behavior implementer" to achieve loose coupling between the two to adapt to changes. Separate the changing and changing factors.
Role Command defines the Command interface and declares the execution method. The ConcreteCommand interface is a virtual implementation. It usually holds the receiver and calls the receiver's function to complete the operations to be executed. Receiver, the object that actually executes the command. Any class may become a receiver, as long as it can implement the corresponding functions required by the command. Invoker requires the command object to execute the request. It usually holds the command object and can hold many command objects. This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object. The Client creates a specific command object and sets the receiver of the command object. Note that this is not a Client in the general sense, but an assembly of command objects and recipients. It may be better to call this Client an assembler, because the client that actually uses the command triggers the execution from Invoker.
Advantage 1. Reduce the coupling between objects. 2. New commands can be easily added to the system. 3. You can easily design a combined command. 4. Call the same method to implement different functions
Disadvantage: using the command mode may cause some systems to have too many specific command classes. Because a specific command class needs to be designed for each command, some systems may need a large number of specific command classes, which will affect the use of the command mode.
Applicability 1. The system needs to decouple the request caller from the request receiver so that the caller and the receiver do not directly interact. 2. The system needs to specify the request, queue the request, and execute the request at different times. 3. The system must support Undo and Redo operations of commands. 4. The system must combine a group of operations to support macro commands.
Application Simulation provides commands for starting, shutting down, and changing TV sets. The Code is as follows:
// Public interface Command {void execute ();}
// Command receiver Receiverpublic class TV {public int currentChannel = 0; public void turnOn () {System. out. println ("The televisino is on. ");} public void turnOff () {System. out. println ("The television is off. ");} public void changeChannel (int channel) {this. currentChannel = channel; System. out. println ("Now TV channel is" + channel );}}
// Boot Command ConcreteCommandpublic class CommandOn implements Command {private TV myTv; public CommandOn (TV TV) {myTv = TV;} public void execute () {myTv. turnOn ();}}
// Shutdown Command ConcreteCommandpublic class CommandOff implements Command {private TV myTv; public CommandOff (TV TV) {myTv = TV;} public void execute () {myTv. turnOff ();}}
// Channel switching Command ConcreteCommandpublic class CommandChange implements Command {private TV myTv; private int channel; public CommandChange (TV TV, int channel) {myTv = TV; this. channel = channel;} public void execute () {myTv. changeChannel (channel );}}
// It can be viewed as the remote Control Invokerpublic class Control {private Command onCommand, offCommand, changeChannel; public Control (Command on, Command off, Command channel) {onCommand = on; offCommand = off; changeChannel = channel;} public void turnOn () {onCommand.exe cute ();} public void turnOff () {offCommand.exe cute ();} public void changeChannel () {changeChannel.exe cute ();}}
// Test class Clientpublic class Client {public static void main (String [] args) {// command Receiver er TV myTv = new TV (); // The command ConcreteCommond CommandOn = new CommandOn (myTv); // The command ConcreteCommond CommandOff off = new CommandOff (myTv ); // channel switching command ConcreteCommond CommandChange channel = new CommandChange (myTv, 2); // command Control object Invoker control Control = new Control (on, off, channel ); // start control. turnOn (); // switch the channel control. changeChannel (); // shutdown control. turnOff ();}}
Execution result
The televisino is on.Now TV channel is 2The television is off.

Conclusion 1. The essence of the command mode is to encapsulate the command and separate the responsibility for issuing the command from that for executing the command. 2. Each Command is an operation: the Requesting Party sends a request and requires an operation. The receiving party receives the request and performs the operation. 3. the command mode allows the requester and the receiver to be independent, so that the requester does not have to know the interfaces of the receiver, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed. 4. The command mode makes the request itself an object, which can be stored and transmitted like other objects. 5. The key to the command mode is to introduce the abstract command interface, and the sender program the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.

More design patterns: 23 Design Patterns

Author: jason0539

Blog: http://blog.csdn.net/jason0539 (reprinted please explain the source)

It is recommended to scan the QR code to pay attention to the public account and add some color to your life.

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.