Lesson 1
Adapter Pattern
Adapter Pattern

Welcome to the first lesson of the course - the Adapter pattern! The Adapter pattern allows objects with incompatible interfaces to work together. It acts as a bridge between two different interfaces. This lesson will help you understand and implement the Adapter pattern in Java.

What You Will Learn

In this lesson, you will learn:

  • The basic concept of the Adapter pattern.
  • How to implement the Adapter pattern with practical code examples.
  • Why the Adapter pattern is useful in software design.
Implementing the Adapter Pattern

In this lesson, we will use an example involving Usb and MicroUsb connections to demonstrate the Adapter pattern. In this example, the problem is a device with a MicroUsb interface that needs to be used where a Usb interface is expected.

  • Target (Usb Interface): This is the interface the client code expects. It defines the method connectWithUsbCable.

  • Adaptee (MicroUsb Class): This is the existing class with an incompatible interface that needs to be adapted. It has the method connectWithMicroUsbCable.

  • Adapter: This is the class that bridges the gap between the Usb interface and the MicroUsb class. The Adapter implements the Usb interface and internally uses an instance of the MicroUsb class to translate the connectWithUsbCable call to connectWithMicroUsbCable.

By implementing the Adapter pattern, the MicroUsb device can now be used where a Usb interface is expected without modifying the existing client code or the MicroUsb class.

The Adapter pattern will help us solve this problem by creating a bridge between these incompatible interfaces so that they can work together seamlessly.

Step 1: Define the Target Interface

First, we define an interface that the client expects to interact with. This is our target interface.

Java
1public interface Usb { 2 void connectWithUsbCable(); 3}

In this example, Usb is the target interface. It declares the connectWithUsbCable method, which the client expects.

Step 2: Create the Adaptee

The adaptee is the existing class with an incompatible interface that needs to be adapted.

Java
1public class MicroUsb { 2 public void connectWithMicroUsbCable() { 3 System.out.println("Connected with MicroUSB cable."); 4 } 5}

Here, MicroUsb is the adaptee. It has a method connectWithMicroUsbCable, which is not compatible with Usb.

Step 3: Implement the Adapter

The adapter class implements the target interface and internally uses an instance of the adaptee class.

Java
1public class Adapter implements Usb { 2 private MicroUsb microUsb; 3 4 public Adapter(MicroUsb microUsb) { 5 this.microUsb = microUsb; 6 } 7 8 @Override 9 public void connectWithUsbCable() { 10 microUsb.connectWithMicroUsbCable(); 11 } 12}

The Adapter class implements Usb and wraps a MicroUsb object. It translates the connectWithUsbCable call into connectWithMicroUsbCable.

Step 4: Use the Adapter

Here is how you can use the adapter to connect a MicroUsb device using a Usb interface.

Java
1public class Main { 2 public static void main(String[] args) { 3 MicroUsb microUsb = new MicroUsb(); 4 Usb adapter = new Adapter(microUsb); 5 adapter.connectWithUsbCable(); // Outputs: Connected with MicroUSB cable. 6 } 7}

In the Main class, we create a MicroUsb object and an Adapter that wraps it. When calling connectWithUsbCable on the adapter, it internally calls connectWithMicroUsbCable of the MicroUsb object.

Importance of the Adapter Pattern

The Adapter pattern is crucial in software design for several reasons:

  • Compatibility: It allows objects with incompatible interfaces to interact.
  • Reusability: You can reuse existing functionalities where interfaces do not match.
  • Flexibility: It provides the flexibility to introduce new interfaces without modifying existing code.

By understanding and using the Adapter pattern, you can design more flexible and reusable systems, improving the maintainability and scalability of your code.

Now that you have a clear understanding of the Adapter pattern, it is time to put this knowledge into practice. Ready to dive into the practice section? Let's get started!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.