适配器模式

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

 

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值