Saturday, 9 November 2013

Adapter Design Pattern – Implementation [Mobile Charger]

🚀 Enjoying Java Tutorials?

Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

🔔 Subscribe to Ram N Java

Introduction

The Adapter Design Pattern is a structural design pattern used to make two incompatible interfaces work seamlessly together. A classic, intuitive real-world analogy is a Mobile Phone Charger: an electrical wall socket provides high voltage (e.g., 120V or 220V), but a mobile phone only requires 9V to charge. Connecting the phone directly to the wall socket is incompatible, so the mobile charger acts as an adapter converting the incoming voltage to what the phone expects.

Adapter Pattern Mapping

Pattern Role Component in Example Description
Client MobilePhone Needs 9V power to charge
Target Interface ITarget Defines get9Volt() method expected by the client
Adaptee ElectricSocket Existing class providing 120V via getVolt()
Adapter MobilePhoneCharger Implements ITarget, fetches 120V, and converts it to 9V

Java Code Implementation

1. The Volt Model Class

public class Volt {
    private int volts;

    public Volt(int volts) {
        this.volts = volts;
    }

    public int getVolts() {
        return volts;
    }

    public void setVolts(int volts) {
        this.volts = volts;
    }
}

2. Adaptee: ElectricSocket

public class ElectricSocket {
    public Volt getVolt() {
        return new Volt(120);
    }
}

3. Target Interface: ITarget

public interface ITarget {
    Volt get9Volt();
}

4. Adapter: MobilePhoneCharger

public class MobilePhoneCharger implements ITarget {
    private ElectricSocket electricSocket = new ElectricSocket();

    @Override
    public Volt get9Volt() {
        Volt v120 = electricSocket.getVolt();
        System.out.println("From Electric Socket, charger got: " + v120.getVolts() + "V");
        
        Volt v9 = convert120VoltTo9Volt(v120);
        System.out.println("Mobile phone charger converted 120V to: " + v9.getVolts() + "V");
        return v9;
    }

    private Volt convert120VoltTo9Volt(Volt v) {
        return new Volt(9);
    }
}

5. Client: MobilePhone

public class MobilePhone {
    public void chargeMe() {
        ITarget target = new MobilePhoneCharger();
        Volt volt = target.get9Volt();
        System.out.println("Mobile phone is charging using: " + volt.getVolts() + "V");
    }

    public static void main(String[] args) {
        MobilePhone mobilePhone = new MobilePhone();
        mobilePhone.chargeMe();
    }
}

Console Output

From Electric Socket, charger got: 120V
Mobile phone charger converted 120V to: 9V
Mobile phone is charging using: 9V

Related Tutorials from Ram N Java


Click here to watch in Youtube : https://www.youtube.com/watch?v=G1CozeYNh48

Click the below Image to Enlarge

Adapter Design Pattern – Implementation [Mobile Charger]






















Adapter Design Pattern – Implementation [Mobile Charger] - Class Diagram




























Volt.java

public class Volt
{
private int volts;

public Volt( int v )
{
this.volts = v;
}

public int getVolts()
{
return volts;
}

public void setVolts( int volts )
{
this.volts = volts;
}
}


ElectricSocket.java

public class ElectricSocket
{
public Volt getVolt()
{
return new Volt(120);
}
}


ITarget.java

public interface ITarget
{
public Volt get9Volt();
}


MobilePhoneCharger.java

public class MobilePhoneCharger implements ITarget
{

private ElectricSocket electricSocket = new ElectricSocket();

@Override
public Volt get9Volt()
{
Volt volt = electricSocket.getVolt();
System.out.println("From ElectricSocket MobilePhoneCharger got :" +volt.getVolts() + "v");
Volt convertedVolt=convertVolt(volt,13);
System.out.println("\nMobilePhoneCharger converterd "+volt.getVolts()+"v to "+convertedVolt.getVolts()+"v\n");
return convertedVolt;
}
private Volt convertVolt(Volt v, int i) 
  {
       return new Volt(v.getVolts()/i);
}

}


MobilePhone.java

public class MobilePhone
{

public static void main(String args[])
{
new MobilePhone().chargeMe();
}
public void chargeMe()
{
ITarget target = new MobilePhoneCharger();
Volt volt=target.get9Volt();
System.out.println("Mobile phone is charging using : "+volt.getVolts() + "v");
}
}


Output

From ElectricSocket MobilePhoneCharger got :120v

MobilePhoneCharger converterd 120v to 9v

Mobile phone is charging using : 9v


3 comments:

  1. Thank you for sharing such an informative message.
    Click here 9 Volt Adapters

    ReplyDelete
  2. A slow or faulty charger can be frustrating, especially when your phone battery drains quickly. Buyerhub.pk offers premium Mobile Chargers in Pakistan, ensuring fast and safe charging for your devices. Don’t step outside – just order online and get it delivered in no time.

    ReplyDelete
  3. The adapter design pattern is a great way to learn how real-world tech solutions work, like how phone chargers can work with different types of plugs. In programming, it helps interfaces that don't work well with each other work together, like how a universal adapter can connect to many devices. This idea is a lot like the tools we use every day. A power bank is another option for when you don't have a charger nearby. It's a great backup that will keep your devices charged anywhere. If you're exploring backup solutions, it's worth checking out the best power bank in Pakistan for reliability, battery capacity, and portability. Both in code and in real life, smart adapters keep things running smoothly.

    ReplyDelete

Tutorials