码迷,mamicode.com
首页 > 其他好文 > 详细

Adapter pattern

时间:2014-07-16 19:01:47      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   

1. 定义 http://en.wikipedia.org/wiki/Adapter_pattern

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. The adapter design pattern is used when you want two different classes with incompatible interfaces to work together. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

2. 分类 http://en.wikipedia.org/wiki/Adapter_pattern

There are two types of adapter patterns:

Object Adapter pattern

In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.

bubuko.com,布布扣bubuko.com,布布扣

Class Adapter pattern

This type of adapter uses multiple polymorphic interfaces to achieve its goal. The adapter is created by implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance.

bubuko.com,布布扣  bubuko.com,布布扣

 

3. 实例 http://blog.chinaunix.net/uid-29140694-id-4138579.html

   1. 类适配器

public interface Target {

    /**
     * 源角色也包含的方法
     */
    public void operation1();
    
    /**
     * 原角色不包含的方法
     */
    public void operation2();
}

/**
 * 原角色实现类
 */
public class Adaptee {

    public void operation1(){
        System.out.println("operation-1");
    }
}

/**
 * 适配器角色
 */
public class Adapter extends Adaptee implements Target{

    @Override
    public void operation2() {
        System.out.println("operation-2");
    }
}

  2. 对象适配器

/**
 * 抽象目标接口
 */
public interface Target {

    /**
     * 购买游戏道具
     */
    public void buyGameProps();
}
/**
 * 被适配对象 
 */
public class Rmb {

    /**
     * 金额属性
     */
    public int money;
    
    /**
     * 构造方法
     * @param money
     */
    public Rmb(int money){
        this.money = money;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }
    
}
/**
 * QQ币实现类(适配器角色)
 */
public class QQCoin implements Target{

    /**
     * 人民币实体对象
     */
    public Rmb rmbImpl;
    
    /**
     * QQ币数量
     */
    public int count;
    
    /**
     * 构造方法
     * @param rmb
     */
    public QQCoin(Rmb rmb){
        this.count = rmb.getMoney() * 10;
    }
    
    /**
     * 具体实现方法
     */
    @Override
    public void buyGameProps() {
        System.out.println("您购买了 " + count + " 个道具!");
    }

}

/**
 * 测试Main方法
 */
public class TestMain {

    public static void main(String [] args){
        Rmb rmb = new Rmb(5);
        QQCoin coin = new QQCoin(rmb);
        coin.buyGameProps();
    }
}

对象适配器的优点:
(1)适配器模式可以理解成是在原有基础上的封装,不需要对原有程序进行改动,即可实现特定功能。
(2)对象适配器可以服务于多个源角色,便于程序的扩展。

 

 

Adapter pattern,布布扣,bubuko.com

Adapter pattern

标签:des   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/davidwang456/p/3844925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!