适配器模式
来源:互联网 发布:数据时代的利与弊作文 编辑:程序博客网 时间:2024/05/18 17:27
适配器模式
组合方式实现
/** * 三相插座接口 */public interface IThreePlug { //使用三相电流供电 void powerWithThree();}
public class GBTwoPlug { //使用二相电流供电 public void powerWithTwo(){ System.out.println("使用二相电流供电"); }}
public class TwoPlugAdapter implements IThreePlug { private GBTwoPlug plug; public TwoPlugAdapter(GBTwoPlug plug) { super(); this.plug = plug; } @Override public void powerWithThree() { System.out.println("通过组合方式转化"); plug.powerWithTwo(); }}
public class NoteBook { private IThreePlug plug; public NoteBook(IThreePlug plug){ this.plug=plug; } /** * 使用插座充电 */ public void charge(){ plug.powerWithThree(); }}
public static void main(String[] args) { GBTwoPlug two = new GBTwoPlug(); IThreePlug plug = new TwoPlugAdapter(two); NoteBook noteBook = new NoteBook(plug); noteBook.charge(); }
继承方式实现
/** * 采用继承方式的插座适配器 */public class TwoPlugClassAdapter extends GBTwoPlug implements IThreePlug{ @Override public void powerWithThree() { System.out.println("借助继承方式转化"); this.powerWithTwo(); }}
public static void main(String[] args) { TwoPlugClassAdapter adapter = new TwoPlugClassAdapter(); NoteBook nb = new NoteBook(adapter); nb.charge(); }
0 0
- 适配器及适配器模式
- 适配器及适配器模式
- 适配器模式(类适配器)
- 适配器模式(默认适配器)
- 适配器模式(对象适配器)
- 适配器模式-类适配器
- 适配器模式-对象适配器
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 适配器模式
- 1、网络爬虫之规则之requests库入门
- Palindrome Number
- git 撤消远程仓库提交
- 线段树,点更新查找
- C++ 指针类型
- 适配器模式
- python PIL 图像处理 (二)
- 热修复之--错误
- MKL学习——向量操作
- PHP swoole实现过10分钟 执行任务
- 玩codecombat游戏,沙漠有一关牧羊人卡住了,求解答
- 剑指offer——用两个栈实现队列
- loadrunner 的安装及汉化
- Java —— 接口可以多继承