Command design pattern: Encapsulates an action into an object, and then creates multiple objects with different operations (both of which perform an identical interface) and encapsulates them into a class, which then invokes different objects according to different incoming arguments to perform their corresponding operations.
The difference between the command design pattern and the policy mode
--the same point:
--there is no doubt that the first of the same points must be: package changes. The policy pattern encapsulates the algorithm's reform, and the command pattern encapsulates the request for change.
--all use the combination to achieve the function, to achieve the purpose of decoupling.
If we do a higher-level abstraction of the command pattern, it can be seen as a strategy pattern. Such as: we will client and
The invoker is packaged together to encapsulate the concrete command with receiver.
--Strategy (strategy) mode:
--The strategy mode is actually relatively simple. The first pattern introduced in Head's Design pattern is the policy model.
--GOF Definition: Define a set of algorithms, wrap them up, and they can replace each other. This mode allows the algorithm to be independent of the
Change with its customers.
--Realize: In fact, a word can explain the essence of the pattern: oriented to abstract programming. The user maintains a unified interface for this series of algorithms,
The interface can be implemented by an algorithm that is interchangeable with each other. The result is decoupling the customer from the specific algorithm implementation.
--Command mode:
--the command pattern is more complex than the policy pattern. This can be found in the command-mode structure diagram (in the post that specifically describes the command pattern)
The number of roles involved could not be verified.
--the command mode involves the following roles: Client, command Interface, Concrete command, invoker and real processing please
And the strategy mode requires only: Client, strategy interface, and concrete strategy.
--GOF definition: encapsulates a request as an object, so that you can parameterize the customer with different requests, queue the request, or
Log request logs, and support revocable operations.
--Implementation: The client creates a command and binds a receiver to the command. Pass the command to a invoker when
When the invoker is awakened, the command's Execute method is invoked, and the Execute method eventually calls the receiver's related method to complete
Request. Of course, a command queue can be maintained in the client, and an undo operation can be implemented in the command. In order to achieve
The revocation and redo of the work.
--Conclusion: the command pattern can be abstracted as a policy pattern. But it is clear that the command pattern deals with more complex situations. Maybe we
It can be said that the policy model focuses on the flexibility of the same request replacement solution, while the command pattern focuses on the changes to multiple requests
The reusability of the package and different request form workarounds for the same request (for example, the text editor presented in GOFDP from different buttons/dishes
Single request to achieve the same functionality).
See:
http://blog.csdn.net/wsh622827/article/details/4759368
Http://www.cnblogs.com/devinzhang/archive/2012/01/06/2315235.html
Http://blog.sina.com.cn/s/blog_67fdef900101g0z3.html
Command design mode