Problem: I have a class that almost does what I need- but I don’t like the access methods.
Solution: Create a wrapper (adapter) around an existing class that you don’t control. The wrapper makes the object conform to your needs.
Example:
The Integer class in Java.
This class wraps itself around the “int” data type.
In this example, we want to create a Circle object. But there exists an object called XX-Circle, which has the required functionality of Circle.
The idea is to create Circle object as a wrapper for XX-Circle hence reusing the functionality of XX-Circle.
Pros
- Only 1 new object, no additional indirection
- Less code required than the object Adapter
- Can override Adaptee's behaviour as required
- Doesn't require sub-classing to work
Cons
- Some time harder to override Adaptee behaviour
- Requires more code to implement properly
Comments