Sec understand design pattern - adapter mode

The two completely different things linked together, just like in real life of the transformer. Assuming that the voltage needed is a mobile phone charger 20V, but the normal voltage is 220V, this time you need a transformer to convert the voltage of 220V to a voltage of 20V, so 20V voltage transformers and mobile phones will be linked up.

 

public class Test {
   public static void main(String[] args) {
       Phone phone = new Phone();
       VoltageAdapter adapter = new VoltageAdapter();
       phone.setAdapter(adapter);
       phone.charge();
   }
}

// 手机类
class Phone {

   public static final int V = 220;// 正常电压220v,是一个常量

   private VoltageAdapter adapter;

   // 充电
   public void charge() {
       adapter.changeVoltage();
   }

   public void setAdapter(VoltageAdapter adapter) {
       this.adapter = adapter;
   }
}

// 变压器
class VoltageAdapter {
   // 改变电压的功能
   public void changeVoltage() {
       System.out.println("正在充电...");
       System.out.println("原始电压:" + Phone.V + "V");
       System.out.println("经过变压器转换之后的电压:" + (Phone.V - 200) + "V");
   }
}



 

Guess you like

Origin www.cnblogs.com/wuwuyong/p/12470246.html