java设计模式:(适配器模式)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wanghang88/article/details/82057042

1:情景

    不同国家的插座,插头不一样,美国的插座,提供110伏电压,三孔插座,中国的插座,提供220伏电压,二孔插座,这个时候你去美国旅游的时候,美国只有三孔插座,这个时候如果没有适配器的话,你就不能给手机充电,所以需要用到适配器,一头转换成二孔插座(给手机充电),另一头插到三孔插座,同理适配器模式也是这么实现的, 下面看具体的实现:

2:java适配器模式的具体实现:

总共7个类 
一个三孔插座接口(Adaptee, 被适配者) 
一个三孔插座类 
一个两孔插座接口(Target, 适配目标) 
一个两孔插座类 
一个适配器(Adapter:实现Target, 组合Adaptee) 
一个手机类(Client) 
一个Main类,用于测试

2.1)三口插座接口:

public interface ThreePinSoket { 

//三孔插座
public void chargeWithThreePin(); 

//电压
public int voltage(); 

}

2.2)三口插座接口的实现

public class ThreePinSoketAmerica implements ThreePinSoket{

    public void chargeWithThreePin(){
        System.out.println("美国标准的三孔的插座");
    }

  
    public int voltage(){
        return 110;      // 美国电压是110伏 
    }

}

2.3)两孔插座接口:

public interface TwoPinSoket{
    //两孔插座
    public void   chargeWithTwoPin();
    
    //两孔的电压 (220v)
    public int    voltage();
}

2.4)两孔插座接口的实现:

public class TwoPinSoketChina implements TwoPinSoket{

    @Override
    public void chargeWithTwoPin(){
        System.out.println("中国标准的两孔的插座");
    }

    @Override
    public int voltage(){
        return 220;      // 中国电压是220伏
    }

}

2.5)创建适配器: 将美国的三孔插座,110v电压装成中国的两孔插座,220v电压(目标是中国的两孔插座);

public class AmericaAdapter implements TwoPinSoket // 实现两孔插座(target){
    
    ThreePinSoket threePinSoket; // 组合三孔插座(adaptee)

    public AmericaAdapter(ThreePinSoket threePinSoket){
        this.threePinSoket = threePinSoket;
    }

    
    public void chargeWithTwoPin(){
        threePinSoket.chargeWithThreePin();
    }

  
    public int voltage(){
      return threePinSoket.voltage() * 2; // 适配器把电压从 110V 升到 220V 
    }

}

2.6)手机:

public class RongYao{
    TwoPinSoket twoPinSoket;

    public RongYao() {
    }

    public void setTwoPinSoket(TwoPinSoket twoPinSoket){
        this.twoPinSoket = twoPinSoket;
    }

    public void chargeRequest(){
        System.out.println("华为荣耀手机, " + twoPinSoket.voltage() + " 伏特充电中\n");
    }

}

2.7)整体的测试类:

public class Main{

    public static void main(String[] args){
        // 在中国,用两孔插座充电
        TwoPinSoketChina twoPinSoketChina = new TwoPinSoketChina();
        RongYao myRongYao = new RongYao();
        myRongYao.setTwoPinSoket(twoPinSoketChina);
        myRongYao.chargeRequest();

        // 然后坐飞机去美国旅游,美国某旅馆的墙上有只有一个三孔插座
        ThreePinSoketAmerica threePinSoketAmerica = new ThreePinSoketAmerica();
        testThreePin(threePinSoketAmerica);

        // 幸好我有美国适配器,一头插到三孔插座,另一头转换成二孔插座,就可以给我的荣耀手机充电
        AmericaAdapter americaAdapter = new AmericaAdapter(threePinSoketAmerica);
        testTwoPin(americaAdapter);

        // 在美国,通过美国适配器,用三空插座充电
        myRongYao.setTwoPinSoket(americaAdapter);
        myRongYao.chargeRequest();

    }

    
   
 public static void testTwoPin(TwoPinSoket twoPinSoket){
        twoPinSoket.chargeWithTwoPin();
        System.out.println("电压是" + twoPinSoket.voltage() + "伏特\n");
    }

 public static void testThreePin(ThreePinSoket threePinSoket){
        threePinSoket.chargeWithThreePin();
        System.out.println("电压是" + threePinSoket.voltage() + "伏特\n");
    }
}

3:在适配器模式中的三个重要的角色:

  • 目标角色(Target),要转换成的目标接口。在我的代码例子中,是中国的两孔接口
  • 源角色(Adaptee),需要被转换的源接口。在我的代码例子中,是美国的三孔接口
  • 适配器角色(Adapter)

猜你喜欢

转载自blog.csdn.net/wanghang88/article/details/82057042