SlideServe
Menu
Préférences
© SlideServe · v2026

Object Adapter Pattern

mcdonaldt avatar
7 diapositives · 111 Views · Jan 05, 2020

Learn how to incorporate a new Japanese phone network into your company's software using the Adapter Pattern efficiently and cost-effectively.

Object Adapter Pattern - slide 1
01 / 7Object Adapter Pattern
1 / 17

Télécharger la présentation

Object Adapter Pattern
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Object Adapter Pattern Danny Leavitt

  2. Imagine... • 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?

  3. Adapter 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.

  4. Object 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.

  5. Back to Phone Company Example

  6. Code Example 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();             }} 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();    }}

  7. When 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