java设计模式——适配器模式 Java源代码

前言:适配器模式就是把一个类的接口变换成客户端所能接受的另一种接口,从而使两个接口不匹配而无法在一起工作的两个类能够在一起工作。通常被用在一个项目需要引用一些开源框架来一起工作时,这些框架的内部都有一些关于环境信息的接口,需要从外部引入,但是外部的接口不一定能匹配,在这种情况下,就需要适配器模式来转换接口。 

 

情景:美国的插座,提供110伏电压;中国的插座,提供220伏电压。

在中国,用两孔插座充电
然后坐飞机去美国旅游,假设美国某旅馆的墙上有只有一个三孔插座
幸好我有美国适配器,一头插到三孔插座,另一头转换成二孔插座,就可以给我的荣耀手机充电
在美国,通过美国适配器,用三空插座充电

图:不同国家的插座,插头不一样,呵呵哒

适配器


图:所以需要写一个适配器模式

适配器类图

 

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

 


 

例子

三孔插座接口(Adaptee)

package adapter;

// adaptee(被适配者) ———— 假设在美国某旅馆的墙上,只有一个三孔插座
public interface ThreePinSoket
{
public void chargeWithThreePin();
public int voltage();
}

 

三孔插座类

package adapter;

// 实现一个具体的 adaptee
public class ThreePinSoketAmerica implements ThreePinSoket
{
    @Override
    public void chargeWithThreePin()
    {
        System.out.println("美国标准的三孔的插座");
    }
    @Override
    public int voltage()
    {
        return 110;      // 美国电压是110伏 
    }
}

 

两孔插座接口(Target)

package adapter;

// client(具体的adaptee) ———— 这个就是我在中国的墙上的两个插孔的插座,我充电只能用这个
public class TwoPinSoketChina implements TwoPinSoket
{
    @Override
    public void chargeWithTwoPin()
    {
        System.out.println("中国标准的两孔的插座");
    }
    @Override
    public int voltage()
    {
        return 220;      // 中国电压是220伏
    }
}

 

适配器(Adapter)

实现Target, 组合Adaptee

package adapter;

// 去美国旅游,必须带上一个“美国适配器”:实现两孔插座,组合三孔插座。用来给我的荣耀手机充电
public class AmericaAdapter implements TwoPinSoket // 实现两孔插座(target)
{
    ThreePinSoket threePinSoket; // 组合三孔插座(adaptee)

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

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

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

}

 

手机类(Client)

package adapter;

public class RongYao
{
    TwoPinSoket twoPinSoket;

    public RongYao() {}

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

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

}

 

Main类,用于测试

package adapter;

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();

    }

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

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

运行结果

华为荣耀手机,

220 伏特充电中

 

 

美国标准的三孔的插座

电压是110伏特

 

美国标准的三孔的插座

电压是220伏特

 

 

华为荣耀手机,

220 伏特充电中

 

分析

适配器模式有三个重要角色:

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

这样,Adaptee和Target两个原本不兼容的接口,就可以在一起工作了(我的荣耀手机就可以在美国充电了)。这里的面向接口编程,得到了松耦合的效果。

美国的三孔插座可以实现Adaptee接口,那么英国、法国的三孔插座也可以去实现Adaptee接口,它们都成为了Adaptee接口的子类。在Adapter类中,由于组合了一个Adaptee的引用,根据Java的多态性,我就可以拿着相同的Adapter类去英国,法国充电了。

另一方面,Client类组合一个Target接口的引用。我们就可制造多个Adapter类,实现同一个Target接口。假设索尼手机的需要日本标准的两孔插座,那么写一个日本两孔插座类实现Target接口,我就可以拿着相同的Adapter类,在美国给日本的索尼手机充电了。



 

转载于:https://www.cnblogs.com/TvvT-kevin/p/10367291.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java适配器设计模式是一种结构型设计模式,用于将一个类的接口转换成客户端所期望的另一种接口,从而使原本接口不匹配的两个类能够协同工作。适配器模式有两种实现方式:类适配器和对象适配器。 类适配器使用继承的方式实现适配器。它包括三个主要角色:目标接口、适配者类和适配器类。目标接口定义了客户端所期望的接口,适配者类是被访问和适配的现存组件库中的组件接口,适配器类继承了适配者类并实现了目标接口,通过继承和重写方法的方式将适配者接口转换成目标接口。 对象适配器使用对象组合的方式实现适配器。它也包括三个主要角色:目标接口、适配者类和适配器类。目标接口和适配者类的定义与类适配器相同,唯一的区别是适配器类不再继承适配者类,而是将适配者对象作为构造参数传入适配器类中,在适配器类的方法中调用适配者对象的方法来实现适配。 适配器设计模式以下场景中适用: 1. 已经存在的类的方法与需求不匹配,需要进行接口转换。 2. 不同产品或不同厂家的类具有相似的功能但接口不相同,需要对它们进行适配。 总结来说,适配器设计模式能够提高类的透明性和复用性,解耦目标类和适配类,提高程序的扩展性。但在编写适配器时需要全面考虑,可能会增加系统的复杂性,并且过度使用适配器可能会导致代码的混乱。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Java设计模式适配器模式](https://blog.csdn.net/qq_37922483/article/details/124568177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [java设计模式-适配器模式](https://blog.csdn.net/weixin_62862983/article/details/122536241)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值