Patterns. Solving a problem using the Adapter pattern in Java and C#. Laboratory work. Adaptation of three methods




Solving a problem using the Adapter pattern in Java and C#. Laboratory work. Adaptation of three methods

It is recommended that you familiarize yourself with the following topic before using this theme:


Contents


Search other websites:

Task

Using the tools of the Java programming language, solve the following problem.

Let a class named Original be given, in which there are 3 methods named OriginalDouble(), OriginalInt(), OriginalChar(). Each of the methods receives one parameter:

  • the OriginalDouble() method receives a double value and displays it on the screen;
  • the OriginalInt() method gets an int value and displays it on the screen;
  • the OriginalChar() method takes a char value and displays it on the screen.

We need to adapt the Original class to the needs of the Client class, which has the following requirements:

  • the OriginalDouble() method must be renamed to ClientDouble(). The method remains unchanged;
  • the OriginalInt() method must be replaced with the ClientInt() method. The ClientInt() method must output the double value (multiplied by 2) of an int parameter;
  • the OriginalChar() method must be replaced with the ClientChar() method. The ClientChar() method outputs a char parameter 5 times.

The Client class must receive a reference to the ITarget interface in the constructor.

 

Solution

1. Solution using Adapter pattern for class
1.1. Block diagram showing the solution of the problem

Figure 1 shows a block diagram showing the solution to this problem using the Adapter pattern for a class.

Adapter pattern for the class. Adaptation of three methods

Figure 1. The solution of the problem. Adapter pattern for the class

 

1.2. Code with Java solution
// The solution of the problem. Using the Adapter pattern for a class.
// The original Original class containing 3 methods:
// OriginalDouble(), OriginalInt(), OriginalChar()
class Original {
  void OriginalDouble(double value) {
    System.out.println("Method Original.OriginalDouble(), value = " + value);
  }

  void OriginalInt(int value) {
    System.out.println("Method Original.OriginalInt(), value = " + value);
  }

  void OriginalChar(char value) {
    System.out.println("Method Original.OriginalChar(), value = " + value);
  }
}

// Declare an ITarget interface containing the names
// of the methods that the client needs
interface ITarget {
  void ClientDouble(double value);
  void ClientInt(int value);
  void ClientChar(char value);
}

// Declare an Adapter class that inherits the Original class
// and implements the ITarget interface
class Adapter extends Original implements ITarget {
  // It is necessary to implement 3 methods of the Original interface
  public void ClientDouble(double value) {
    // here call the OriginalDouble() method - name replacement
    this.OriginalDouble(value);
  }

  public void ClientInt(int value) {
    // Print the double value of type int
    this.OriginalInt(value*2);
  }

  public void ClientChar(char value) {
    // Print 5 times the value
    for (int i=0; i<5; i++)
      OriginalChar(value);
  }
}

class Client {
  // A reference to the client
  private ITarget client;

  // Class constructor
  Client(ITarget _client) {
    client = _client;
  }

  // Method that invokes all methods of the interface
  void Show() {
    client.ClientDouble(2.88);
    client.ClientInt(39);
    client.ClientChar('f');
  }
}

public class TestOrigin {
  public static void TestClient() {
    // Testing client performance - another way
    // 1. Declare a reference to the ITarget interface
    ITarget client;

    // 2. Implement adaptation
    client = (ITarget) new Adapter();

    // 3. Demonstrate how the methods work
    client.ClientDouble(3.77);
    client.ClientInt(255);
    client.ClientChar('z');
  }

  public static void main(String[] args) {
    // Declare the instance of Adapter class
    Adapter adapter = new Adapter();

    // Declare an instance of the Client class and pass an adapter instance to it
    Client client = new Client(adapter);

    // Invoke method Show()
    client.Show();
  }
}

 

1.3. The text of the program with the solution in C#

 

// The Adapter pattern for the class. Solution in C#
using System;
using static System.Console;

namespace ConsoleApp10
{
  // Original class, which contains 3 methods
  // OriginalDouble(), OriginalInt(), OriginalChar()
  class Original
  {
    public void OriginalDouble(double value)
    {
      WriteLine("Method Original.OriginalDouble(), value = {0}", value);
    }

    public void OriginalInt(int value)
    {
      WriteLine("Method Original.OriginalInt(), value = {0}", value);
    }

    public void OriginalChar(char value)
    {
      WriteLine("Method Original.OriginalChar, value = {0}", value);
    }
  }

  // Declare the ITarget interface,
  // which contains the names of the methods that the client needs
  interface ITarget
  {
    void ClientDouble(double value);
    void ClientInt(int value);
    void ClientChar(char value);
  }

  // Adapter class - implements the ITarget interface, inherits the Original class
  class Adapter : Original, ITarget
  {
    public void ClientDouble(double value)
    {
      // in the ClientDouble() method, call the OriginalDouble() method
      OriginalDouble(value);
    }

    public void ClientInt(int value)
    {
      OriginalInt(value * 2);
    }

    public void ClientChar(char value)
    {
      for (int i = 0; i < 5; i++)
        OriginalChar(value);
    }
  }

  // Class Client - receives a reference to the ITarget interface in the constructor
  class Client
  {
    private ITarget client; // reference to the ITarget interface

    // Constructor
    public Client(ITarget _client)
    {
      client = _client;
    }

    // Method that invokes all methods of the interface
    public void Show()
    {
      client.ClientDouble(2.88);
      client.ClientInt(39);
      client.ClientChar('f');
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Demonstration of the Adapter pattern for the class.
      // 1. Declare an instance of Adapter class
      Adapter adapter = new Adapter();

      // 2. Declare an instance of the Client class and pass an adapter instance to it
      Client client = new Client(adapter);

      // 3. Invoke the Show() method of client
      client.Show();
    }
  }
}

 

1.4. The program result
Method Original.OriginalDouble(), value = 2.88
Method Original.OriginalInt(), value = 78
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f

 

2. Solution using the Adapter pattern for the object
2.1. A block diagram showing the solution of the problem

Figure 2 shows a block diagram that shows the solution to this problem using the Adapter pattern for an object.

The Adapter pattern for an object. Adaptation of three methods

Figure 2. The Adapter pattern for an object. Structural scheme

 

2.2. The text of the program with the decision of the Java language

Below is the text of the module that displays the solution to the problem using the Adapter for object pattern.

// The solution of the problem. Using the Adapter pattern for an object.
// The Original class containing 3 methods:
// OriginalDouble(), OriginalInt(), OriginalChar()
class Original {
  void OriginalDouble(double value) {
    System.out.println("Method Original.OriginalDouble(), value = " + value);
  }

  void OriginalInt(int value) {
    System.out.println("Method Original.OriginalInt(), value = " + value);
  }

  void OriginalChar(char value) {
    System.out.println("Method Original.OriginalChar(), value = " + value);
  }
}

// Declare an ITarget interface containing the names of the methods
// that the client needs
interface ITarget {
  void ClientDouble(double value);
  void ClientInt(int value);
  void ClientChar(char value);
}

// Declare an Adapter class that implements the ITarget interface
class Adapter implements ITarget {
  private Original obj; // Declare a reference to Original

  // Class constructor - receives a reference to Original
  Adapter(Original _obj) {
    obj = _obj;
  }

  // It is necessary to implement 3 methods of the Original interface
  public void ClientDouble(double value) {
    // here call the OriginalDouble () method - name replacement
    obj.OriginalDouble(value);
  }

  public void ClientInt(int value) {
    // Print a double value of type int
    obj.OriginalInt(value*2);
  }

  public void ClientChar(char value) {
    // Print 5 times value
    for (int i=0; i<5; i++)
      obj.OriginalChar(value);
  }
}

// Client class
class Client {
  // Reverence to client
  private ITarget client;

  // Class constructor
  Client(ITarget _client) {
    client = _client;
  }

  // Method that calls all methods of the interface
  void Show() {
    client.ClientDouble(2.88);
    client.ClientInt(39);
    client.ClientChar('f');
  }
}

public class TestOrigin {
  public static void main(String[] args) {
    // Declare an instance of the Original class to adapt
    // to the requirements of the Client class
    Original obj = new Original();

    // Declare an instance of the Adapter class, initialized with the Original instance
    Adapter adapter = new Adapter(obj);

    // Declare an instance of the Client class and pass an adapter instance to it
    Client client = new Client(adapter);

    // Invoke method Show() of class Client
    client.Show();
  }
}

 

2.3. The text of the program with the solution in C#

The text of the solution in C# is not much different from the solution in Java.

// The Adapter pattern for an object. Solution in C#
using System;
using static System.Console;

namespace ConsoleApp10
{
  // Source class Original, which contains 3 methods
  // OriginalDouble(), OriginalInt(), OriginalChar()
  class Original
  {
    public void OriginalDouble(double value)
    {
      WriteLine("Method Original.OriginalDouble(), value = {0}", value);
    }

    public void OriginalInt(int value)
    {
      WriteLine("Method Original.OriginalInt(), value = {0}", value);
    }

    public void OriginalChar(char value)
    {
      WriteLine("Method Original.OriginalChar, value = {0}", value);
    }
  }

  // Declare the ITarget interface, which contains the names of the methods
  // that the client needs
  interface ITarget
  {
    void ClientDouble(double value);
    void ClientInt(int value);
    void ClientChar(char value);
  }

  // The Adapter class - implements the ITarget interface.
  // The class receives as an input parameter a reference to Original in the constructor.
  class Adapter : Original, ITarget
  {
    // Reference to the class to be adapted
    private Original obj;

    // Class constructor
    public Adapter(Original obj)
    {
      this.obj = obj;
    }

    public void ClientDouble(double value)
    {
      // In the ClientDouble() method invoke the OriginalDouble() method
      obj.OriginalDouble(value);
    }

    public void ClientInt(int value)
    {
      obj.OriginalInt(value * 2);
    }

    public void ClientChar(char value)
    {
      for (int i = 0; i < 5; i++)
        obj.OriginalChar(value);
    }
  }

  // Client class - receives a reference to the ITarget interface in the constructor
  class Client
  {
    private ITarget client; // reference to the ITarget interface

    // Constructor
    public Client(ITarget _client)
    {
      client = _client;
    }

    // Method that calls all methods of the interface
    public void Show()
    {
      client.ClientDouble(2.88);
      client.ClientInt(39);
      client.ClientChar('f');
    }
  }

  class Program
  {
    static void Main(string[] args)
    {
      // Demonstration of pattern Adapter for a class.
      // 1. Declare an instance of class Original
      Original obj = new Original();

      // 2. Declare an instance of the Adapter class, pass obj to it
      Adapter adapter = new Adapter(obj);

      // 3. Declare an instance of the Client class and pass the adapter instance to it
      Client client = new Client(adapter);

      // 4. Invoke method Show()
      client.Show();
    }
  }
}

 

2.4. The result of the program
Method Original.OriginalDouble(), value = 2.88
Method Original.OriginalInt(), value = 78
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f
Method Original.OriginalChar(), value = f

 


Related topics