Design Patterns of Command
Abstract: Model of Command
Command mode is the most Let me puzzled a model, I read a lot of code, only vaguely understand the feeling about principle, I think the most important thing is to understand design principles have been constructed, suchæ‰å¯¹programming guide their actual role . Command mode is not really a very specific, many provisions of the model, it is this flexibility, some people confuse.
Many Command Command definition of the model code are directed at the graphical interface, it is actually the menu command, we have a drop-down menu to select a command, and will then perform some action.
These orders will be packaged as a category, then users (call) of this type to operate, and this is Command mode, in other words, have users (call) is the direct transfer of these orders, such as the menu open Documentation (call), open the file directly at the code, use Command mode, which is an intermediate between the increase, this will be directly related to Yuduan, isolation between the two are basically no the relations.
Clearly the benefits of this package is in line with the characteristics of reducing coupling, Command will conduct package is the typical pattern Factory is to create a package model,
From the Command Mode, I also found design a "common problem": It seems like a simple issue will be complicated, like an increase in the different types of third party, of course, to do so robust maintainability of the code are reusable sexual.
How to use it?
Command mode code specific variety, because how packaging orders, different systems, different approaches. Below are examples of packages in the order of a Collection List, any object once it joins the List, in a packed closed black box, the object of the disappeared, only removed when will it be possible to distinguish between the fuzzy:
Typical of the need for a Command mode interface. Interfaces in a unified way, and this is "an order / request package targeting":
(Public interface Command
Public abstract void execute ();
)
Different specific order / request of the code is to achieve Interface Command, the following three specific orders
Public class Engineer implements Command (
Public void execute () (
/ / Do Engineer's command
)
)
Public class Programmer implements Command (
Public void execute () (
/ / Do programmer's command
)
)
Public class Politician implements Command (
Public void execute () (
/ / Do Politician's command
)
)
As is the usual practice, we will be able to directly call these three Command, but the use of Command mode, we have to package them up, throw went black box List:
(Public class producer
Public static List produceRequests () (
List queue = new ArrayList ();
Queue.add (new DomesticEngineer ());
Queue.add (new Politician ());
Queue.add (new Programmer ());
Return queue;
)
)
These three orders entered in the List, has lost its characteristic appearance, later removed, but also may not be able to tell who is who is the Engineer Programmer, see below how to call Command mode:
(Public class TestCommand
Public static void main (String [] args) (
List queue = Producer.produceRequests ();
For (Iterator it queue.iterator = (); it.hasNext ();)
/ / Retrieve List of Dongdong, other characteristics are not sure, a feature only guarantee is 100% correct,
/ / Command Interface is at least a "son." Therefore, mandatory conversion to type Command Interface
((Command) it.next ()). Execute ();
)
)
Therefore, only basic caller interface and dealing with substandard concrete realization of interactive, which also embody a principle-oriented programming interface, so that after the fourth increase in specific order, would not have to modify the caller TestCommand of the code.
Understand the above code of the core principles in use, they should each have their own methods, particularly in how to separate and specific orders call, a lot of method, the above code is the use of "off again from List" approach. only to demonstrate this approach.
Command mode of use of a good reason but also because it can achieve Undo function. Each specific commands can be executed just remember it moves, and restored if needed.
Command model widely used in interface design. Java Swing in the menu commands are using Command mode, as in the Java interface design there is a lack of performance, interface design specific code we have not discussed, the Internet has many such examples.
↑ Back
Tags: the design pattern






