适配器模式( 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设计中的两个重要模式:抽象类与适配器模式。 首先,我们来理解一下“抽象”。抽象是面向对象编程的核心概念之一,它帮助我们处理对象的复杂性。在Java中,抽象通常通过抽象类来实现。抽象类...
在这个Java实现中,我们将深入探讨适配器模式的两大类型:类适配器模式和对象适配器模式,并通过具体的代码示例和UML类图来阐述其工作原理。 首先,我们来理解适配器模式的基本概念。适配器模式就像现实生活中的...
适配器模式是一种常用的设计模式,它在软件工程中扮演着重要的角色,特别是在解决系统间的兼容性和接口不匹配问题时。适配器模式的核心思想是将一个类的接口转换成客户希望的另一个接口,使原本由于接口不兼容而无法...
适配器模式是一种常用的设计模式,它在软件工程中扮演着重要的角色,特别是在解决系统间的兼容性和接口不匹配问题时。适配器模式的核心思想是将一个类的接口转换成客户希望的另一个接口,使原本由于接口不兼容而无法...
Java动态代理模式与适配器模式是两种在软件设计中常用的设计模式,它们都有各自的优点和应用场景。在Java中,动态代理模式主要依赖于Java的反射API和InvocationHandler接口,而适配器模式则用于解决不同接口之间的...
适配器模式是一种在软件工程中广泛使用的结构型设计模式,它允许两个不兼容的接口之间进行通信。在Java中,适配器模式扮演着重要的角色,尤其在处理遗留代码或者第三方库集成时,能够有效地解决接口不匹配的问题。...
适配器模式是一种设计模式,它允许不兼容的类或接口之间进行通信,通过创建一个适配器类作为中间桥梁,使得原本不匹配的接口能够协同工作。在Java中,适配器模式广泛应用于系统集成、旧代码复用以及第三方库的兼容性...
适配器模式是一种软件设计模式,它允许两个不兼容的接口之间进行通信。在这个案例中,我们关注的是如何通过适配器模式解决实际编程问题。文章《适配器模式案例代码》提供了具体的实现示例,链接指向了CSDN博主...
适配器模式是一种常用的设计模式,它在软件工程中扮演着重要的角色,特别是在处理系统集成、遗留代码重用以及不同接口之间兼容性问题时。适配器模式的主要目的是将两个不兼容的接口融合在一起,使得原本无法直接协作...
适配器模式是一种常用的设计模式,它在软件开发中起到了桥梁的作用,允许两个不兼容的接口之间进行通信。在这个“适配器模式demo源码”中,我们可以深入理解这一模式的实现方式及其应用场景。 适配器模式的核心思想...
适配器模式是一种软件设计模式,它在不同的接口之间起到了桥梁的作用,使得原本由于接口不兼容而无法协作的类能够协同工作。这种模式的核心思想是将一个类的接口转换成客户希望的另一个接口,从而使原有类能适应新的...
适配器模式是一种常用的设计模式,它在软件工程中扮演着重要的角色,允许不同接口的类之间进行通信。适配器模式的核心思想是将一个类的接口转换成客户期望的另一个接口,使得原本由于接口不兼容而无法一起工作的类...
适配器模式是一种软件设计模式,它允许两个不兼容的接口之间进行通信。在本实验中,我们将深入探讨适配器模式的概念、应用场景以及如何在实际编程中实现它。适配器模式通常分为类适配器和对象适配器两种形式。 ...
适配器模式是一种常用的设计模式,它在软件工程中扮演着重要的角色,允许不兼容的接口之间进行通信。在这个源代码实例中,我们看到的是如何通过适配器模式来实现不同对象之间的协作,使得原本无法直接交互的系统组件...
适配器模式(Adapter)是软件工程中一种常用的设计模式,它允许两个不兼容的接口之间进行通信。在C++编程中,适配器模式能够帮助我们复用现有的类,或者将第三方库的接口与我们的系统接口进行对接,从而提高代码的可...