Adapter Design Pattern in Java

The Adapter Design Pattern in Java is a structural design pattern that allows two incompatible interfaces to work together by creating a third object, called an adapter, that acts as a bridge between them. The pattern allows objects with incompatible interfaces to collaborate without modifying their source code.

The Adapter Design Pattern in Java has the following properties:

  1. Compatibility: The Adapter Design Pattern is used to make two incompatible interfaces work together. It is often used when working with legacy code or third-party libraries that use an interface that is incompatible with the application’s interface.
  2. Interface translation: The pattern achieves compatibility by creating an adapter that translates the interface of one object to the interface expected by another object.
  3. Reusability: The Adapter Design Pattern allows existing classes to be reused without modifying their source code. The adapter class can be used to adapt multiple incompatible classes to work with the target interface.
  4. Simplification: The pattern simplifies the use of complex interfaces by providing a simpler interface that is easier to use.
  5. Dependency reduction: The Adapter Design Pattern reduces the dependency between the client and the adaptee by introducing an intermediate adapter class.
  6. Two types of adapter: There are two types of adapters: object adapters and class adapters. Object adapters use composition to adapt the interface of an object, while class adapters use multiple inheritance to adapt the interface of a class.

In Java, the Adapter Design Pattern can be implemented in the following way:

  1. Create an interface or abstract class that represents the target interface.
    public interface Target {
        void request();
    }
  2. Create a class that implements the target interface.
    public class ConcreteTarget implements Target {
        @Override
        public void request() {
            System.out.println("ConcreteTarget.request()");
        }
    }
  3. Create an interface or abstract class that represents the incompatible interface.
    public interface Adaptee {
        void specificRequest();
    }
  4. Create a class that implements the incompatible interface.
    public class ConcreteAdaptee implements Adaptee {
        @Override
        public void specificRequest() {
            System.out.println("ConcreteAdaptee.specificRequest()");
        }
    }
  5. Create an adapter class that implements the target interface and uses an instance of the incompatible interface.
    public class Adapter implements Target {
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        @Override
        public void request() {
            adaptee.specificRequest();
        }
    }
  6. Use the adapter class to call the method of the incompatible interface through the target interface.
    public class Main {
        public static void main(String[] args) {
            Target target = new ConcreteTarget();
            target.request();
    
            Adaptee adaptee = new ConcreteAdaptee();
            Target adapter = new Adapter(adaptee);
            adapter.request();
        }
    }

    In this example, the ConcreteTarget class represents the target interface, and the ConcreteAdaptee class represents the incompatible interface. The Adapter class implements the target interface and uses an instance of the incompatible interface to call its specific method. The Main class uses both the target and the adapter interface to call their respective methods.

Overall, the Adapter Design Pattern in Java allows two incompatible interfaces to work together by creating a third object, called an adapter, that acts as a bridge between them. The pattern is useful when working with legacy code or third-party libraries that cannot be modified to conform to the interface used by the application. By creating an adapter, the application can use the incompatible interface without modifying its source code.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial