问题
Proxy pattern delegates the request to the Real subject after doing some additional processing like applying checks if request needs to be processed or not based on may be some credential checks.
It has class diagram as below
Proxy class has a direct reference to the concrete Subject.
Decorator Pattern enriches the behavior of the component [like proxy it also does some additional processing and delegates the operation to the real component]. The class diagram of this pattern is similar to Proxy pattern with only difference being it has the reference to the interface of the component.
Having concrete real subject in Proxy class makes it difficult for unit testing as Classes should only be dependent upon interfaces and not the implementations. My Question is if Proxy pattern also has the reference to the interface exposed by the Real subject then will it be equivalent to the Decorator pattern. In such a case the class diagram of the proxy pattern will also become as below
回答1:
It's all about the intent. Functionally, they will be equivalent, but the point of decorator is to add features to an object dynamically, while proxy just controls the access to the target object without adding any additional features to it.
So the client of the proxy expects the same outcome as if it worked with the real object, while the client of the decorator leaves it to the decorator to execute any additional logic before and/or after delegating the call to the target.
So, conceptually it seems that in your example you're still dealing with a proxy.
回答2:
Yes. If you look at structure, it will be same for both Decorator and Proxy. Only intent is different.
Decorator:
- Add behaviour to object at run time. Inheritance is the key to achieve this functionality, which is both advantage and disadvantage of this pattern.
- It modifies the behaviour of interface.
e.g. (with chaining ) : java.io package classes related to InputStream
& OutputStream
interfaces
FileOutputStream fos1 = new FileOutputStream("data1.txt");
ObjectOutputStream out1 = new ObjectOutputStream(fos1);
Consequences
- Decoration is more convenient for adding functionalities to objects instead of entire classes at runtime. With decoration it is also possible to remove the added functionalities dynamically.
- Decoration adds functionality to objects at runtime which would make debugging system functionality harder.
Proxy:
- Use it for lazy initialization, performance improvement by caching the object and controlling access to the client/caller. It may provide alternative behaviour or call real object. During this process, it may create new Object.
- Unlike Decorator, which allows chaining of objects, Proxy does not allow chaining.
e.g.: java.rmi package classes
Key takeaways:
- Proxy provides the same interface. Decorator provides an enhanced interface.
- Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
Useful links:
Decorator_pattern from wikiepdia
decorator from sourcemaking
decorator-pattern from oodesign
Proxy_pattern from wikipedia
proxy from sourcemaking
proxy-pattern from oodesign
来源:https://stackoverflow.com/questions/36266690/if-in-proxy-pattern-we-have-interface-instead-of-actual-concrete-subject-in-prox