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.
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.
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 theMicroUsb
class. TheAdapter
implements theUsb
interface and internally uses an instance of theMicroUsb
class to translate theconnectWithUsbCable
call toconnectWithMicroUsbCable
.
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.
First, we define an interface that the client expects to interact with. This is our target interface.
Java1public interface Usb { 2 void connectWithUsbCable(); 3}
In this example, Usb
is the target interface. It declares the connectWithUsbCable
method, which the client expects.
The adaptee is the existing class with an incompatible interface that needs to be adapted.
Java1public 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
.
The adapter class implements the target interface and internally uses an instance of the adaptee class.
Java1public 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
.
Here is how you can use the adapter to connect a MicroUsb
device using a Usb
interface.
Java1public 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.
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!