适配器模式( adater)
1目的:将一个类的接口转化成客户类所需要的接口,使原本因为接口不兼容而不能一起工作的类能够一起给on工作.
2适配器的分类:对象适配器和类适配器(通过多继承对一个接口和另外一个接口适配),java因为只支持单继承,所以这里只讲对象适配器.
3何时需要适配器模式
3.1 当你想使用一个已经存在的类,但是他的接口不符合你所需要的.
3.2 你想使用现有的几个子类,但是通过适配每个子类的接口来子类化是不切实际.一个对象适配器可以适配这几个子类的父类.
3.3 你想创建一个与 无关的或者不可预见的类 合作的可复用类,但是这个可重复类不一定需要可以兼容的接口.
4 实例
package adater; public class FishingBoat { public void sail(){ System.out.println("the fishingboat is move to that place"); } public void fish() { System.out.println(" fishing"); }
package adater; public class Captain implements BattleShip { private BattleShip battleShip; public Captain(){ } public Captain(BattleShip battleShip){ this.battleShip=battleShip; } @Override public void fire() { battleShip.fire(); } @Override public void move() { battleShip.move(); } public BattleShip getBattleShip() { return battleShip; } public void setBattleShip(BattleShip battleShip) { this.battleShip = battleShip; } }
package adater; public interface BattleShip { void fire(); void move(); }
package adater; public class BattleFishingBoat implements BattleShip { private FishingBoat boat; public BattleFishingBoat(){ boat=new FishingBoat(); } public void fire() { System.out.println("fire!"); } @Override public void move() { boat.sail(); } }
package adater; /** * An adapter helps two incompatible interfaces to work together. This is the real world definition * for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. * The Adapter design pattern allows otherwise incompatible classes to work together by converting * the interface of one class into an interface expected by the clients. * * <p> * There are two variations of the Adapter pattern: The class adapter implements the adaptee's * interface whereas the object adapter uses composition to contain the adaptee in the adapter * object. This example uses the object adapter approach. * * <p> * The Adapter ({@link BattleFishingBoat}) converts the interface of the adaptee class ( * {@link FishingBoat}) into a suitable one expected by the client ( {@link BattleShip} ). * * <p> * The story of this implementation is this. <br> * Pirates are coming! we need a {@link BattleShip} to fight! We have a {@link FishingBoat} and our * captain. We have no time to make up a new ship! we need to reuse this {@link FishingBoat}. The * captain needs a battleship which can fire and move. The spec is in {@link BattleShip}. We will * use the Adapter pattern to reuse {@link FishingBoat}. * */ public class App { public static void main(String[] args) { Captain captain=new Captain(new BattleFishingBoat()); captain.move(); captain.fire(); } }
相关推荐
java设计模式【之】适配器模式【源码】【场景:水和容器】 * 适配器模式,类似(亡羊补牢) * 已经存在的类(功能已经开发完成了),与新需求的功能相似,但是略有差异 * 当已经存在的方法无法满足现有需求,需要...
C#设计模式之适配器模式应用实例
设计模式之适配器模式Java版 设计模式之适配器模式Java版 设计模式之适配器模式Java版 设计模式之适配器模式Java版
[刚刚上传的demo运行有问题,重新传了个新的],这个的一个iOS的适配器模式的实例代码,目前就是一个简单的demo,后期还会更新适配器在 MVC,MVP,MVVM中的使用,demo对应着简书:https://www.jianshu.com/p/e2ebcf19098f,...
实验7 适配器模式、外观模式与模板方法模式---Java设计模式
适配器模式的Java实现版本,包含类适配器模式和对象适配器模式的实现,同时也包含两者的UML类图
动态代理模式 适配器模式 实现,都可以运行,有一部分注解
设计模式之适配器模式,通过实际例子讲述适配器模式的使用,原理和特点
适配器模式代码demo 适配器模式代码demo 适配器模式代码demo
适配器模式 设计模式 Adapter 还请各位大S指教
设计模式-适配器模式(讲解及其实现代码)
设计模式--适配器模式java例子
设计模式C++学习之适配器模式(Adapter)
实验七:适配器模式.rar实验七:适配器模式.rar
设计模式的适配器模式的例子,希望对大家有用
设计模式系列之适配器模式,分别以Java,Android,IOS进行讨论