I was looking at the Proxy Pattern, and to me it seems an awful lot like the Decorator, Adapter, and Bridge patterns. Am I misunderstanding something? What's the difference? Why would I use the Proxy pattern versus the others? How have you used them in the past in real world projects?
相关问题
- Name for a method that has only side effects
- What is a correct approach to manage test data usi
- Can a [GoF]-ConcreteSubject override the notify me
- Qt Webkit Bridge: C++ access to JavaScript
- Can the builder pattern ever be doing too much?
相关文章
- Builders in Java versus C++?
- Can the staticmethod and classmethod decoraters be
- Class decorators in ES7
- “Adapter” or “adaptor”?
- Is copy-and-paste coding ever acceptable?
- How to Create a Custom tabBarController to simulat
- Error building angular+ngrx 8 for production when
- Dependency Injection Container
Design pattern is not mathematics, it is combination of art and software engineering. There is nothing like for this requirment you have to use proxy, bridge etc. Design patterns are created to solve the problems. If you anticipate a design problem, then use it. Based on experience, you will come to know for specific problem, which pattern to use. If you are good in solid design principles, you would have implemented design pattern without knowing it is pattern. Common example is statergy and factory patterns
Hence concentrate more on solid desighn principles, clean coding principles and ttd
All of the four patterns involve wrapping inner object/class with outer one, so they are very similar structurally. I would outline difference by the purpose:
And by interface variation between inner and outer objects:
I use it quite often when consuming web services. The Proxy Pattern should probably be renamed to something more pragmatic, like 'Wrapper Pattern". I also have a library that is a Proxy to MS Excel. It makes it very easy to automate Excel, without having to worry about background details such as what version is installed (if any).
Speaking detail implementation, I find a difference between Proxy and Decorator, Adapter, Facade ... In common implementation of these patterns there's a target object wrapped by a enclosing object. Client uses enclosing object instead of target object. And the target object actually play an important part inside some of methods of enclosing object.
However, in case of Proxy, enclosing object can play some methods by itself, it just initialize target object when client calls some methods that it needs target object take part in. This is lazy initialization. In case of other patterns, enclosing object is virtually based on target object. So target object is always initialized along with enclosing object in constructors/setters.
Another thing, a proxy does exactly what a target does whereas other patterns add more functionality to target.
This is quote from Head First Design Patterns
Definitions belongs to book. Examples belongs to me.
Decorator - Doesn’t alter the interface, but adds responsibility. Assume you have a car interface, when you implement this for different model of the car (s, sv, sl) you may need to add more responsibility for some models. Like has sunroof, airbag etc..
Adapter - Converts one interface to another. You have a car interface and you would like it to act like jeep. So you take the car, modify it and turn into a jeep. Since it is not a real jeep. But acts like a jeep.
Facade - Makes an interface simpler. Assume you have car, airplane, ship interfaces. Actually all you need is a class which sends people from one location to another. You want facade to decide what vehicle to use. Then you collect all those interface references under 1 umbrella and let it decide/delegate to keep it simple.
Head First: "A facade not only simplifies an interface, it decouples a client from a subsystem of components. Facades and adapters may wrap multiple classes, but a facade’s intent is to simplify, while an adapter’s is to convert the interface to something different."
I would like to add examples to Bill Karwing answer (which is great btw.) I add also some key differences of implementation, that I feel are missing
Quoted parts are from answer of [https://stackoverflow.com/a/350471/1984346] (Bill Karwing)
ProxyClass and ObjectClass that is proxied, should implement same interface, so they are interchangable
Example - proxy expensive object
DecoratorClass should(could) implement extended interface of ObjectClass. So the ObjectClass could be replaced by DecoratorClass, but not vice versa.
Example - adding addition functionality
Implentation differences Proxy, Decorator, Adapter
Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
Most of the information in this answer is from https://sourcemaking.com/design_patterns, which I recommend as an excellent resource for design patterns.