I was reading the implementation of method Collections.synchronizedList() and was confused whether its an example of decorator pattern or a proxy pattern?
The synchronizedList() method of java. util. Collections class is used to return a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
We can use Collections. synchronizedList(List<T>) method to synchronize collections in java. The synchronizedList(List<T>) method is used to return a synchronized (thread-safe) list backed by the specified list.
e.g. ArrayList, LinkedList. Returns a Synchronized(thread-safe) list backed by the specified list. The parameter list is the list to be wrapped in a synchronized list.
In order to get a synchronized list from an ArrayList, we use the synchronizedList(List <T>) method in Java. The Collections. synchronizedList(List <T>) method accepts the ArrayList as an argument and returns a thread safe list.
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.
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.
As per URL https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html, Collections.synchronizedList() is example of decorator pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With