Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What pattern is used in Collections.synchronizedList()

I was reading the implementation of method Collections.synchronizedList() and was confused whether its an example of decorator pattern or a proxy pattern?

like image 349
Chander Shivdasani Avatar asked Sep 13 '13 19:09

Chander Shivdasani


People also ask

What is the use of collections synchronizedList?

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.

How do you make a collection synchronized in Java?

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.

What type of list is used with synchronized access *?

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.

How do I find 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.


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:

  • Examples of GoF Design Patterns in Java's core libraries
  • Decorator Pattern for IO
like image 137
BalusC Avatar answered Oct 18 '22 07:10

BalusC


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.

like image 40
Gray Avatar answered Oct 18 '22 08:10

Gray


As per URL https://docs.oracle.com/javase/tutorial/collections/implementations/wrapper.html, Collections.synchronizedList() is example of decorator pattern.

like image 28
asingh Avatar answered Oct 18 '22 08:10

asingh