Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of a ForwardingMap in Guava? [closed]

Tags:

java

guava

I am scratching my head over understanding the use of a ForwardingMap? What are the cases that one might use it?

like image 593
Hossein Avatar asked Jan 29 '13 12:01

Hossein


1 Answers

ForwardingXxx classes provide decorator pattern implementations for all JDK and Guava collections, including Map.

Read more on Guava's wiki and in Effective Java 2nd Edition, Item 16: Favor composition over inheritance:

To summarize, inheritance is powerful, but it is problematic because it violates encapsulation. It is appropriate only when a genuine subtype relationship exists between the subclass and the superclass. Even then, inheritance may lead to fragility if the subclass is in a different package from the superclass and the superclass is not designed for inheritance. To avoid this fragility, use composition and forwarding instead of inheritance, especially if an appropriate interface to implement a wrapper class exists. Not only are wrapper classes more robust than subclasses, they are also more powerful.

Basically it lets you customize possibly non-extendable Maps without adding dependencies on actual Map implementation.

like image 164
Xaerxess Avatar answered Oct 21 '22 08:10

Xaerxess