Design patterns

The Adapter pattern

Adapter is a struc­tur­al design pat­tern that allows objects with incom­pat­i­ble inter­faces to collaborate.

Adapter is a struc­tur­al design pat­tern that allows objects with incom­pat­i­ble interfaces to collaborate. The adapter imple­ments the inter­face of one object and wraps the other one.

The Client Inter­face describes a contract that other class­es must fol­low to work with the client code. The Adaptee is some use­ful (for example 3rd-party) class. The client can’t use this class direct­ly because of an incom­pat­i­ble interface. The Adapter is a class that is able to work with both the client and the ser­vice (adaptee). It imple­ments the client inter­face, while wrap­ping the ser­vice object. The adapter receives calls from the client via the adapter inter­face and trans­lates them into calls to the ser­vice object. The client code doesn’t get cou­pled to the con­crete adapter class because it works via the client inter­face. When the inter­face of the ser­vice gets changed or services is replaced you only need to cre­ate a new adapter class with­out chang­ing the client code.

How it works (code examples):

public interface UngroundedPlug {
    public getFirstPinPower;
    public getSecondPinPower();
}
public interface GroundedPlug {
    public getFirstPinPower;
    public getSecondPinPower();
    public getGroundPinPower();
}
public class UkPlug implements GroundedPlug{
    private integer firstPinPower;
    private integer secondPinPower;
    private integer groundPinPower;

    public class UkPlug (integer firstPinPower, integer secondPinPower, integer groundPinPower) {
        this.firstPinPower = firstPinPower;
        this.secondPinPower = secondPinPower;
        this.groundPinPower = groundPinPower;
    }

    public getFirstPinPower() {
        System.out.println("First pin power" + firstPinPower);
    }

    public getSecondPinPower() {
        System.out.println("Second pin power" + secondPinPower);
    }

    public getGroundPinPower() {
        System.out.println("Ground pin power" + groundPinPower);
    }
}
public class EuPlug implements UngroundedPlug{
    private integer firstPinPower;
    private integer secondPinPower;

    public class EuPlug (integer firstPinPower, integer secondPinPower) {
        this.firstPinPower = firstPinPower;
        this.secondPinPower = secondPinPower;
    }

    public getFirstPinPower() {
        System.out.println("First pin power" + firstPinPower);
    }

    public getSecondPinPower() {
        System.out.println("Second pin power" + secondPinPower);
    }
}
public class EuPlugAdapter implements GroundedPlug {
    private EuPlug euPlug;

    public EuPlugAdapter(EuPlug euPlug) {
        this.euPlug = euPlug;
    }

    public getFirstPinPower() {
        euPlug.getFirstPinPower();
    }

    public getSecondPinPower() {
        euPlug.getSecondPinPower();
    }

    public getGroundPinPower() {
        System.out.println("Earthing is not implemented");
    }
}
public class AdapterTest {
    public static void main(String[] args) {
        GroundedPlug groundedPlug = new UkPlug(230, 0, 0);
        EuPlug euPlug = new EuPlug(0, 220);
        GroundedPlug euPlugAdapter = new EuPlugAdapter(euPlug);
        
        System.out.println("UK plug voltage:")
        testPlug(groundedPlug);

        System.out.println("EuPlugAdapter voltage:")
        testPlug(euPlugAdapter);
    }

    static void testPlug(GroundedPlug groundedPlug){
        groundedPlug.getFirstPinPower();
        groundedPlug.getSecondPinPower();
        groundedPlug.getGroundPinPower();
    }
}

Related patterns: Facade, Bridge, State, Strategy, Decorator and Proxy.

Leave a Reply

Your email address will not be published. Required fields are marked *