I was reading the implementation of method Collections.synchronizedList() and was confused whether its an example of decorator pattern or a proxy pattern?
3 Answers
It's definitely a decorator. It wraps the provided list with a different implementation of the very same interface whose methods alters the behavior (by synchronizing the access) of the very same underlying collection while delegating the methods straight to the wrapped instance.
If it were a proxy pattern, you would not necessarily need to pass the to-be-wrapped collection during construction and those methods would under the covers not necessarily refer exactly the same collection instance on every call.
See also:
-
A decorator pattern that uses a proxy to modify the wrapped class? – Gray Sep 13 '13 at 19:47
-
@Gray: it's definitely not a proxy. The underlying instance of a proxy is usually created "on demand" (more than often via lazy loading). If you're familiar with EJBs, you'll understand that part better. – BalusC Sep 13 '13 at 19:49
-
I use the term "proxy" to mean network proxying of interfaces -- more like delegation proxying. These are not "on demand" but tend to wrap handlers to provide RPC connections. I have no experience with EJB thank god. :-) – Gray Sep 13 '13 at 19:52
-
@Gray: The word "proxy" in question and answer must be interpreted as design pattern not as network application. As to your subjective confirmation of your inexperience with EJBs, it look like that you're still hanging in the old J2EE / EJB 2.x era (they were then indeed extremely terrible to create, use and maintain) and you actually never looked at EJBs as they are since Java EE 5 / EJB 3.0 which was introduced a little more than 7 years ago. Please catch up the moving technologies. – BalusC Sep 13 '13 at 19:56
-
Can't tell if you are joking. I've always been a POJO Java guy. Never even worked in a servlet environment. Maybe it's time that you caught up? ;-) – Gray Sep 13 '13 at 20:00
-
1In terms of design patterns, isn't AOP an example of a proxy pattern _and_ a decorator pattern? I would argue that you definition of proxy pattern may need to be expanded. – Gray Sep 13 '13 at 20:01
-
1+1 I would have said the delegation pattern was used too, because it's not just overriding methods to add functionality - it has a reference to, and invokes methods on, the wrapped collection. – Bohemian Sep 13 '13 at 20:29
-
@BalusC, isnt proxy just another way of decorating an object? – Chander Shivdasani Sep 13 '13 at 20:56
-
@Chander: nope, proxy doesn't alter behavior. It just points out the right instance at the moment you need it and delegates the method call straight to it without the need to worry about creating and managing the instance(s) yourself. Decorator pattern alters behavior. – BalusC Sep 13 '13 at 20:58
-
@BalusC Spring AOP is what really got me confused. Its an implementation of proxy pattern and it also adds extra responsibility to the object. I think the one reason that makes this a decorator pattern is that we are adding responsibility on a need-by-basis(dynamically). While in Proxy, the client is unware of the object that Proxy is hiding. – Chander Shivdasani Sep 13 '13 at 21:15
-
@Chander: AOP isn't exactly a design pattern. AOP is a programming paradigm. Say, a way to achieve something. More than often decorator pattern is been used in the path, but it can also be as good observer pattern. – BalusC Sep 13 '13 at 21:27
-
@Bohemian [Decoration is a form of delegation](http://stackoverflow.com/a/13393686/829571). – assylias Sep 13 '13 at 21:39
I was reading the implementation of method Collections.synchronizedList() and was confused whether its an example of decorator pattern or a proxy pattern?
I agree with @BalusC that it is certainly a decorator which is modifying the functionality of a wrapped object. But I would argue that it also demonstrates aspects of a proxy pattern -- at least how I use the term.
The wikipedia page on the Proxy Pattern gives as part of its definition "class functioning as an interface to something else".
In looking at this definition of the proxy design pattern, the author defines it as "Provide a surrogate or placeholder for another object to control access to it."
I think this is what the Collections.synchronizedList()
wrapper code is doing by calling through to the delegate:
public E get(int index) {
return list.get(index);}
public E set(int index, E element) {
throw new UnsupportedOperationException();
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
public int indexOf(Object o) {
return list.indexOf(o);
}
In this answer, How do the Proxy, Decorator, Adapter, and Bridge Patterns differ?, the author defines decorators as "Smart Proxy" patterns which I think sounds right. Then there are Aspect Oriented Programming and other similar patterns use both "proxy" and "decoration" in explaining how they work.
Certainly there are proxy patterns which are lazy loaded or sparse in their support for the underlying object's methods and functionality. There are proxy patterns which remote certain portions of an object to a RPC handler which I would argue is a form of decoration.
-
Exactly my confusion. But after reading Balus's answer and few more resources online, i think its a decorator pattern. In proxy, most of the time, the real object is hidden from the client and is created at compile time. Decorator, on the other hand, adds responsibility to the object dynamically and the client is fully aware of the object that is being decorated. Take a look at this link: http://powerdream5.wordpress.com/2007/11/17/the-differences-between-decorator-pattern-and-proxy-pattern/ – Chander Shivdasani Sep 13 '13 at 21:22
-
1Yeah I think that's one way of looking at it but I'm not sure all decorators are dynamically generated. Certainly I use the term decorator object that I hand wire or have Spring wire for me @ChanderShivdasani. – Gray Sep 13 '13 at 21:27
As per URL https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html, Collections.synchronizedList() is example of decorator pattern.

- 157
- 2
- 5