zoukankan      html  css  js  c++  java
  • Adapter pattern

    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.

    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.

      

    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)对象适配器可以服务于多个源角色,便于程序的扩展。

  • 相关阅读:
    Golang Gin使用模板及框架下返回Json
    Golang Web下返回HTML内容处理方法
    Golang net.http和Gin框架下的模板--View调用后台方法 --view接收后台数据的运用 及 嵌套模板和继承模板的使用
    设置oracle编辑的快捷方式
    oracle中的异常处理方法
    游标的使用
    网站登录界面包数据库异常
    在PLSQL中不能使用中文作为查询条件查询数据
    oracle错误一览表
    oracle导入时报错
  • 原文地址:https://www.cnblogs.com/davidwang456/p/3844925.html
Copyright © 2011-2022 走看看