适配器模式-Adapter Pattern

1. 类适配器

  • 客户端接口不满足要求
  • 自定义一个适配器接口以及具体的适配器类,转换原来客户端接口
  • 消费客户端时,客户端的实现不可见,具体是有适配器类进行工作
/** 1. 房屋买卖,买方不和卖房直接沟通,所有事情都是中介和
       卖方沟通,然后将信息回馈给买方
    2. 买方只和中介打交道  
*/
public class Test01 {
    public static void main(String[] args) {
        // 使用系统提供的适配器,也可以自定义适配器
        Buyer.buyHouse(new MiddleAdapter());
    }
}

// 客户端类: 只和最原始的适配器打交到,具体传入的类型,可以让客户端自己定义
class Buyer {
    public static void buyHouse(Adapter adapter) {
        System.out.println(adapter.bargain());
    }
}

// 适配器类: 继承源类,实现适配器接口
class MiddleAdapter extends SourceHouseSeller implements Adapter {

    @Override
    public String bargain() {
        int originalPrice = super.getPrice(); // 可以不用super,只是为了可读性
        int finalPrice = originalPrice / 5;
        return "ha, now the price is " + finalPrice + "rmb";
    }
}

// 适配器接口
interface Adapter {
    String bargain();
}

// 源类
class SourceHouseSeller {

    public int getPrice() {
        return 100;
    }
}
  • 单继承原因,适配器类必须继承源客户端,这就要求适配器必须是一个接口
  • 尽量减少继承

2. 对象适配器

  • 将继承改为组合

public class Test01 {
    public static void main(String[] args) {
        Buyer.buyHouse(new MiddleAdapter());
    }
}

class Buyer {
    public static void buyHouse(Adapter adapter) {
        System.out.println(adapter.bargain());
    }
}

class MiddleAdapter implements Adapter {

    private SourceHouseSeller sourceHouseSeller = new SourceHouseSeller();

    @Override
    public String bargain() {
        int originalPrice = sourceHouseSeller.getPrice();
        int finalPrice = originalPrice / 5;
        return "ha, now the price is " + finalPrice + "rmb";
    }
}

interface Adapter {
    String bargain();
}

class SourceHouseSeller {

    public int getPrice() {
        return 100;
    }
}

3. 缺省适配器模式

  • 如果适配器接口中包含很多方法,但是在具体的适配器中并没有用到,这样就需要重写很多没用的方法
  • 可以使用java 8 引入的接口默认实现方式,使得适配器中每个方法都有默认实现,具体的适配器需要用到哪个方法,就去覆盖哪个方法,省去了不必要方法的覆盖
public class Test01 {
    public static void main(String[] args) {
        Buyer.buyHouse(new MiddleAdapter());
    }
}

class Buyer {
    public static void buyHouse(Adapter adapter) {
        System.out.println(adapter.bargain());
    }
}

class MiddleAdapter implements Adapter {

    private SourceHouseSeller sourceHouseSeller = new SourceHouseSeller();

    @Override
    public String bargain() {
        int originalPrice = sourceHouseSeller.getPrice();
        int finalPrice = originalPrice / 5;
        return "ha, now the price is " + finalPrice + "rmb";
    }
}

interface Adapter {
    String bargain();

    /**
     * JDK8 的接口默认实现
     * @return
     */
    default int getAge() {
        return -1;
    }

    default String getName(){
        return "";
    }
}

class SourceHouseSeller {

    public int getPrice() {
        return 100;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值