适配器模式
来源:互联网 发布:免费下载绘图软件 编辑:程序博客网 时间:2024/06/08 16:02
适配器模式(结构型模式)
Adapter模式也叫适配器模式,可以改变已有类的接口
例子:我们国家的电压标准为220v(已经存在的类),而我的电脑是接受不了这么大的电压,这时候我们的电源适配器(适配器)就起作用了,它可以将220v电压转化为你想要的电压
有两种实现方法,一是继承那个类,二是引用那个类
Current.java
public class Current { public void useCurrent() { System.out.print("使用220v电压!"); }}
Adapter.java
电压适配器,继承方式
public class Adapter extends Current { public void useWantCurrent() { this.useCurrent(); System.out.println("转换之后的电压可以供电脑使用!"); }}
Adapter2.java
电压适配器,引用方式
public class Adapter2 { private Current current; public Adapter2(Current current) { this.current = current; } public void useCurrent() { current.useCurrent(); System.out.println("第二种方法使用!"); }}
Test.java
测试类
public class Test { public static void main(String[] args) { Adapter adapter = new Adapter(); adapter.useWantCurrent(); //引用方式 Adapter2 adapter2 = new Adapter2(new Current()); adapter2.useCurrent(); }}
结果
使用220v电压!转换之后的电压可以供电脑使用!使用220v电压!第二种方法使用!
阅读全文
0 0
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- leetcode-4-Median of Two Sorted Arrays
- 2017暑假集训 div1 连通图(2)
- “玲珑杯”ACM比赛 Round #18 C -- 图论你先敲完模板
- 判断链表是否有交点若有找出交点
- JSON详细学习之Gson in JAVA
- 适配器模式
- 冒泡排序简单改进
- python基础之异常处理、调试和正则表达式
- 表单
- Prim 模板
- [状态压缩||折半搜索]51nod 算法马拉松3 A
- 数组2
- VC6.0加载lib文件的三种方法
- 十进制转二进制