适配器模式开发

 Adapter模式也叫适配器模式,是构造型模式之一,通过Adapter模式可以改变已有类(或外部类)的接
口形式。

在大规模的系统开发过程中,我们常常碰到诸如以下这些情况:我们需要实现某些功能,这些功能已有还不太成熟的一个或多个外部组件,

如果我们自己重新开发这些功能会花费大量时间;所以很多情况下会选择先暂时使用外部组件,

以后再考虑随时替换。避免代码大面积修改
Adapter模式就是针对这种类似需求而提出来的。
Adapter模式通过定义一个新的接口(对要实现功能加以抽象),
和一个实现该接口的Adapter(适配器)类来透明地调用外部组件。
这样替换外部组件时,最多只要修改几个Adapter类就可以了,其他
源代码都不会受到影响。


package com.org.adapter02;
public class Adapter extends Current{
    public void use18V() {
        System.out.println("使用适配器");
        this.use220V();
    }
}


package com.org.adapter02;
public class Adapter2 {
    private Current current;
    public Adapter2(Current current) {
        this.current = current;
    }
    public void use18V() {
        System.out.println("使用适配器");
        this.current.use220V();
    }
}


package com.org.adapter02;
public class Current {
    public void use220V() {
        System.out.println("使用220V电流");
    }
}

      


public class MainClass {
    public static void main(String[] args) {
//        Current current = new Current();
//        current.use220V();
        
//        Adapter adapter = new Adapter();
//        adapter.use18V();
        
        Adapter2 adapter = new Adapter2(new Current());
        adapter.use18V();
    }
}



转载于:https://my.oschina.net/goudingcheng/blog/521897

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值