`

适配器模式

 
阅读更多

                                                        适配器模式( 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();
    }
}

 

  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics