适配器模式
适配器模式(Adapter):将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
双方都不太容易修改的时候再使用适配器模式。
系统的数据和行为都正确,但接口不符时,我们应该考虑用适配器器,目的是使得控制范围之外的一个原有对象与某个接口匹配。适配器模式主要应用于希望复用一些现在的类,但是接口又与复用环境要求不一致的情况。适配器模式分两种类型:类适配器模式 和 对象适配器模式。
类的适配器模式(采用继承实现)
对象适配器(采用对象组合方式实现)
Adaptee类
package adapter; /** * 已存在的、具有特殊功能、但不符合我们希望的接口的类 * @author 煞笔 * */ public class Adaptee { public void specificRequest(){ System.out.println("已经存在的特殊功能,被适配"); } }
Target类
package adapter; /** * 目标接口 * @author 煞笔 * */ public interface Target { public void request(); }
Adapter类
package adapter; /** * 类适配器模式(采用继承实现) * 适配器类,继承了被适配类,同时实现标准接口 * @author 煞笔 * */ public class Adapter extends Adaptee implements Target { /** * 实现目标接口 */ @Override public void request() { //super表示调用父类的方法 super.specificRequest(); } }
AdapterObject类
package adapter; /** * 对象适配器模式(采用对象组合方式实现) * @author 煞笔 * */ public class AdapterObject implements Target { private Adaptee adaptee = new Adaptee(); /** * 或者通过构造函数传入具体需要适配的被适配类对象 public AdapterObject(Adaptee adaptee) { this.adaptee = adaptee; } */ @Override public void request() { adaptee.specificRequest(); } }
Business类
package adapter; public class Business { public static void main(String[] args) { //1、类适配器模式 Adapter adapter = new Adapter(); adapter.request(); //2、对象适配器模式 AdapterObject adapterObject = new AdapterObject(); adapterObject.request(); } }
再举例对象适配器
Player类
package adapter.basketball; public abstract class Player { public String name; public abstract void attack(); public abstract void defense(); public Player(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
ForwardsPlayer类
package adapter.basketball; public class ForwardsPlayer extends Player { public ForwardsPlayer(String name) { super(name); } @Override public void attack() { System.out.println("前锋"+name+"进攻"); } @Override public void defense() { System.out.println("前锋"+name+"防守"); } }
CenterPlayer类
package adapter.basketball; public class CenterPlayer extends Player { public CenterPlayer(String name) { super(name); } @Override public void attack() { System.out.println("中锋"+name+"进攻"); } @Override public void defense() { System.out.println("中锋"+name+"防守"); } }
GuardsPlayer类
package adapter.basketball; public class GuardsPlayer extends Player { public GuardsPlayer(String name) { super(name); } @Override public void attack() { System.out.println("后卫"+name+"进攻"); } @Override public void defense() { System.out.println("后卫"+name+"防守"); } }
TransPlayer类
package adapter.basketball; public class TransPlayer extends Player { private ForeignCenterPlayer ym = new ForeignCenterPlayer("姚明"); public TransPlayer(String name) { super(name); } @Override public void attack() { ym.进攻(); } @Override public void defense() { ym.防守(); } }
ForeignCenterPlayer类
package adapter.basketball; public class ForeignCenterPlayer { public String name; public ForeignCenterPlayer(String name) { this.name = name; } public void 进攻() { System.out.println("外籍中锋"+name+"进攻"); } public void 防守() { System.out.println("外籍中锋"+name+"防守"); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Business类
package adapter.basketball; public class Business { public static void main(String[] args) { Player a = new ForwardsPlayer("AA"); a.attack(); Player c = new CenterPlayer("CC"); c.defense(); Player g = new GuardsPlayer("GG"); g.attack(); Player f = new TransPlayer("FF"); f.attack(); } }