Title: Object Adapter Pattern
1Object Adapter Pattern
2Imagine...
- You program for the control center of a US phone
company. Your network managment software is
Object Oriented with a simple hierarchy...  - Â
- Â
- Â
- Â
- Â
- Â
- Â Â
- Your company acquires a Japanese phone company.Â
The workings of a Japanese network are complex
and foreign to you, but you need to incorporate
this new network into your software. Â - Â
- The Japanese company used similar software with a
NetworkUtilities class containing a StartNetwork
operation rather than InitializeNetwork. You are
only given a compiled version of the class. - Â
- Do you begin studying Japanese communication
protocols and waste millions of company dollars
implementing your own InitializeNetwork algorithm?
3Adapter Pattern (aka Wrapper)
- The adapter pattern is a design pattern that is
used to allow two incompatible types to
communicate. Where one class relies upon a
specific interface that is not implemented by
another class, the adapter acts as a translator
between the two types. - Â
- 3 essentially classes involved Â
- Â Target - class that needs to steal operations
from some other class (Adaptee) - Adapter - class that wraps the operations of the
foreign class (Adaptee) in Target-familiar
interfaces - Adaptee - class with operations desired for the
Target class - Â
- In Class Adapter, this was accomplished by having
the Adapter inherit from both the Target and the
Adapter.
4Object Adapter Pattern
- In Object Adapter, the Adapter subclasses the
Target. Instead of subclassing the Adaptee, the
Adapter internally holds an instance of the
Adaptee and uses it to call Adaptee operations
from within operations familiar to the Target.
5Back to Phone Company Example
6Code Example
- Â Â Â
- Â
- Â
- Â
- Network.java - the target
- public abstract class Network    abstract
void InitializeNetwork() - Â
- Â
- Â
- Â
- Â
- JapaneseNetwork.java - the adapter
- Â
- public class JapaneseNetwork extends Network
   //instance of the adaptee   -     NetworkUtilities myAdapteeObject Â
 public void JapaneseNetwork(NetworkUtilities nu)
      myAdapteeObject nu   - Â
- Â Â Â public void InitializeNetwork() Â Â Â Â Â Â
myAdapteeObject.StartNetwork()Â Â Â
NetworkUtilities.java - the adaptee  public
class NetworkUtilities    public void
StartNetwork() Â Â Â Â Â Â //complex Japanese
network code... Â Â Â Â Tester.java - the
client  public class Tester    public void
Tester() Â Â Â Â Â Â NetworkUtilities nu new
NetworkUtilities() Â Â Â Â Â Â JapaneseNetwork
jNetwork new JapaneseNetwork(nu)Â Â Â Â Â Â Â
USNetwork uNetwork new USNetwork() Â Â Â Â Â
        //both networks use familiar interface
but jNetwork uses               //adaptee
      uNetwork.InitializeNetwork()      Â
jNetwork.InitializeNetwork() Â Â Â Â Â Â Â Â Â
7When to use Object Adapter?
- you want to use an existing class, and its
interface does not match the one you need - Â
- you want to create a reusable class that
cooperates with unrelated or unforeseen classes,
that is, classes that don't necessarily have
compatible interfaces. - Â
- you have several subclasses and would like to
adapt some of their operations. Use Object
Adapter to adapt their parent class instead of
adapting all subclassesÂ