structural comparison
To be honest, when the blogger first learns the complete design patterns, these three design patterns alone which is very clear and clear, but with the increase in the type of pattern, in the actual use of the time there will be a trance, for example, read the open source code, encountered no pattern naming specification of the code, it is difficult to say that the specific use of these three kinds of which is one of them.
The reason for the confusion is that the implementation of all three patterns is based on the idea of interface orientation; all three patterns are implemented for a specific interface and provide indirect access to another object with an object on its implementation class.
Based on the point of confusion, the adapter pattern would be the first to be distinguished from the class diagram, theClass structure diagram of an adapter:
From the class diagram, the protagonist of the adapter is the class Adapter, which is in an implementation relationship with the target interface, theBut Adaptee is different, it can be an interface or a concrete class, it is an object on a level with Target and has nothing to do with Targetand the Adapter's relationship to it isobject composition, is an internal member variable of Adaptee as an Adapter, the purpose of Adapter is to adapt Adaptee to Target.
The Proxy and Decorator patterns are more easily confused than the Adapter pattern because they are both based on a public interface and the protagonist classes (Proxy and Decorator) both perform secondary operations on the target objects (RealSubject and ConcreteComponent) in the form of composite objects. Implementation-wise, they are very similar.
But note that there's still a difference here.
- Proxy pattern is a proxy object (Proxy) to the actual object being proxied (RealSubject) is to provide indirect access to the implementation class of the public interface;
- The decorator class of the Decoration pattern provides indirect access to the decorated interface, or decorated base class (Component), not to the implementation class of the interface (Component).
In other words.The two provide indirect access to different objects, one (the proxy pattern) is based on concrete classes, while the other (the decoration pattern) is based on abstract interfaces .This is why the request object, which can be used only once, is encapsulated based on the decorative pattern to achieve secondary use (for details, see the
essential comparison
The Nature of the Adapter Pattern
The main function of the adapter pattern is to perform conversion matching, the purpose is to reuse the existing functions, rather than to implement new interfaces. That is to say, the client needs the function should be already implemented, do not need the adapter mode to realize, the adapter mode is mainly responsible for the incompatible interface into the client's expectations on the look.
The essence of an adapter is to convert functions to improve reusability.Unlike proxies and decorations, the target object in an adapter may have nothing to do with the object that needs to be adapted, and the adapting process is even a full rewrite of the code, which aims to merge the two contents along one of the main.
The Nature of the Proxy Model
Proxy pattern is to create a proxy object, the proxy object to represent the real object, the client to get the proxy object, the client does not have any impact on the client, as with the real object to get the same to use. When the client operates the proxy object, the actual function will eventually be completed by the real object, only through the proxy operation, that is, the client operates the proxy, the proxy operates the real object.
It is because there is a proxy object sandwiched between the client and the real object being proxied, which is equivalent to a transit, then there are a lot of tricks you can play in the transit, for example, to determine the permissions, if there is not enough permissions that do not carry out the transit, or transit to other operations.
The operational logic of the proxy model:
The essence of the proxy pattern is to control object access.The core lies in the Proxy role, similar to the middleman, is the target object of a "front spokesman", you want to access the target object, or to buy the target manufacturer's goods, you must go from my agent here, I can even not create the object to be proxied, they pretend to be the object of the proxy (directly to the labeled products as the target product to sell) I can even pretend to be an agent myself without creating an agent (and sell the target product directly under a label).
The Nature of the Decoration Pattern
The Decoration pattern enables the dynamic addition of functionality to an object, adding functionality to an object from outside the object, which is equivalent to changing the appearance of the object. When decorated, from the point of view of the external use of the system, it is no longer the original object that is used, but the object that has been decorated by a series of decorators. This gives the flexibility to change the functionality of an object, and as soon as the dynamically assembled decorators are changed, the functionality of the resulting object is also changed. From this point of view, the proxy pattern can also be satisfied, but the difference is that the decorative pattern has a principle that the proxy can not transgress, that is, the proxy pattern must have a target object, otherwise there is no point in decorating.
The essence of the decorative pattern is to enhance the capabilities of the target object by combining them, which is the extension of the object's capabilities.Decorative patterns are based on the original object of the outer layer of packaging, you can target the object of each method are before, after the supplement and enhancement, but in essence does not change the original object, is a kind of extension of the thinking, which can be referred to the idea of AOP.