Saturday, 9 November 2013

Adapter Design Pattern – Implementation [Mobile Charger]


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