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

设计模式07-适配器模式

时间:2014-06-23 06:00:32      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   tar   ext   

1. 概念

      将一个类的接口转换成客户希望的另外一个接口  Adapter模式使得原本由于接口不兼容而不能在一起工作的那些类可以在一起工作

         1.Target

                定义Client使用的与特定领域相关的接口。
       
 2.Client
       
       与符合Target接口的对象协同。
        
3.Adaptee
               
定义一个已经存在的接口,这个接口需要适配。
   
     4.Adapter
             对Adaptee的接口与Target接口进行适配

 

2. 案例 

         

package org.demo.adapter02;
/**
 * think in patterns 中的例子 
 * Function : 
 * @author : Liaokailin
 * CreateDate : 2014-3-4
 * version : 1.0
 */
public class Demo02 {
    public static void main(String[] args) {
        Target t = new Adapter(new Adaptee()) ;
        t.request() ;
    }
}

/**
 * 
 * 客户端需要调用的操作 
 */
class Target{
    public void request(){} ;
}

/**
 * 
 * 客户端需要访问的接口  但是该接口由于不兼容不能直接给客户调用  因此需要增加一个适配
 */
class Adaptee{
    public void specificRequest(){
        System.out.println("Adaptee:SpecificRequest.");
    }
}

/**
 * 适配器
 *   将Adaptee 转换为客户端识别的数据
 */
class Adapter extends Target{
    private Adaptee adaptee ;
    public Adapter(Adaptee adaptee){
        this.adaptee = adaptee ;
    }
    public void request(){
        System.out.println("----将adaptee转化为客户端识别的数据-----");
        adaptee.specificRequest() ;
    }  
}

 

设计模式07-适配器模式,布布扣,bubuko.com

设计模式07-适配器模式

标签:style   class   blog   code   tar   ext   

原文地址:http://www.cnblogs.com/liaokailin/p/3799804.html

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