Patterns. Examples of implementation of the Adapter pattern

Java. Examples of implementation of the Adapter pattern

This topic is a continuation of the topic:


Contents





Search other websites:

1. The structure of pattern Adapter

Figure 1 and Figure 2 show the structure of the Adapter pattern for a class and an object, respectively. The following paragraphs implement the implementation of these structures in Java language.

The Adapter pattern for a class

Figure 1. The Adapter pattern for a class

The Adapter pattern for an object

Figure 2. The Adapter pattern for an object

 

2. An example of implementation of the Adapter pattern for a class in Java

This example is a demo and implements the structure shown in Figure 1.

// Adapter pattern. Implementing a pattern for a class
// Adapts the Adaptee class to the Target interface via the Adapter intermediate class
// Interface ITarget
interface ITarget {
  void Request();
}

// The class whose method needs to be adapted to another system.
// In this case, adapting the name of the SpecificRequest () method to the Request() method
class Adaptee {
  // Some specific method
  void SpecificRequest() {
    System.out.println("Adaptee.SpecificRequest()");
  }
}

// Adapter class - implements the ITarget interface and inherits the Adapter class
class Adapter extends Adaptee implements ITarget {
  // Implementation of the Request() method of the ITarget interface
  public void Request() {
    // In the body of the method, the Specific Request() method of the Adapter() class is called
    SpecificRequest();
  }
}

// Client class - receives a reference to the ITarget interface
class Client {
  // some method of class Client
  void ClientMethod(ITarget target) {
    target.Request();
  }
}

public class TestAdapter {
  public static void main(String[] args) {
    // Demonstration of the Adapter pattern for a class..
    // Task.
    // You need to adapt the SpecificRequest() method of the Adaptee class to the needs of the Client instance.
    // 1. Let some instance of the Client class be given
    Client client = new Client();

    // 2. Create an instance of the Adapter class that references the ITarget interface
    ITarget target = (ITarget) new Adapter();

    // 3. Pass target reference to client method
    client.ClientMethod(target);
  }
}

The client can be implemented differently – without using the Client class. To do this, just declare a reference to the ITarget interface. The following code demonstrates another use case for the client using the ITarget interface.

...

public class TestAdapter {
  public static void main(String[] args) {
    // Demonstration of the Adapter pattern for the class.
    // Task.
    // We need to adapt the SpecificRequest () method of the Adaptee class to the needs of the client instance
    // Implementation without using the Client class
    // 1. Declare a reference to the ITarget interface
    ITarget client;

    // 2. Create an instance of the Adapter class that refers to the ITarget interface
    ITarget target = (ITarget) new Adapter();

    // 3. Pass the target reference to the client
    client = target;

    // 4. Invoke the Request() method
    client.Request(); // Adaptee.SpecificRequest()
  }
}

The above implementation can be simplified even further.

public class TestAdapter {
  public static void main(String[] args) {
    // 1. Declare a reference to the ITarget interface and adapt it to the client's needs
    ITarget client = (ITarget) new Adapter();

    // 2. Invoke method Request()
    client.Request(); // Adaptee.SpecificRequest()
  }
}

 

3. An example of implementation of the Adapter pattern for an object. Solution in Java

This example is demo and implements the structure shown in Figure 2.

// Pattern Адаптер. Implementing a pattern for an object
// Interface Target
interface ITarget {
  void Request();
}

// The class whose method needs to be adapted to another system.
// In this case, adapting the name of the SpecificRequest() method to the Request() method
class Adaptee {
  // Some specified method
  void SpecificRequest() {
    System.out.println("Adaptee.SpecificRequest()");
  }
}

// Adapter class - implements the ITarget interface and inherits the Adapter class
class Adapter implements ITarget {
  // A reference to an instance of the Adapter class is declared
  private Adaptee adaptee;

  // Constructor - gets a reference to Adaptee
  Adapter(Adaptee adaptee) {
    this.adaptee = adaptee;
  }

  // Implementation of the Request() method of the ITarget interface
  public void Request() {
    // In the middle of the method, the SpecificRequest() method of the adapter instance is called
    adaptee.SpecificRequest();
  }
}

// Class Client - gets a reference to the ITarget interface
class Client {
  // Method of class Client
  void ClientMethod(ITarget target) {
    target.Request();
  }
}

public class TestAdapter {
  public static void main(String[] args) {
    // Demonstration of the Adapter pattern for an object.
    // Task.
    // You need to adapt the Adaptee instance to the needs of the Client instance.
    // 1. Let some instances of Client and Adaptee classes be given
    Client client = new Client();
    Adaptee adaptee = new Adaptee();

    // 2. Create an instance of class Adapter
    ITarget target = (ITarget) new Adapter(adaptee); // pass the instance Adaptee to Adapter

    // 3. Pass target to the client
    client.ClientMethod(target);
  }
}

The client can also be implemented in a different way.

...

public class TestAdapter {
  public static void main(String[] args) {
    // Demonstration of the Adapter pattern for an object.
    // Task.
    // It is necessary to adapt an instance of the Adaptee class to the needs of the client instance
    // Another way to implement a client is without using the Client class
    // 1. Let an instance of the Adaptee class be given, which needs to be adapted
    Adaptee adaptee = new Adaptee();

    // 2. Declare a reference to the interface - this is the client
    ITarget client;

    // 3. Pass an adaptee instance to the client through an intermediary - an instance of the Adapter class
    Adapter adapter = new Adapter(adaptee); // create the instance of class Adapter
    client = (ITarget)adapter;

    // 4. Invoke method Request()
    client.Request(); // Adaptee.SpecificRequest()
  }
}

 


Related topics