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

设计模式之--适配器模式(Adapter)

时间:2015-02-15 16:23:36      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

定义:Convert a interface of class into anthoer interface clients expect.
     Adapter let classes work together that could‘t otherwise because of incompatible interfaces
   将实现某种接口的类转换成客户端希望的另一种接口接收方式.适配器让原本由于实现不同接口而无法一起应用的类在一起工作。

其他:适配器分为类适配器 和 对象适配器
     类适配:适配器通过继承第三方类,来整合第三方的资源到客户端需要的类中
     对象适配器:适配器通过聚合第三方的类,来整合地方的资源到客户端需要的类中(常用)

对象适配器类图:
技术分享

 

一:接口

package dmode.adapter;

/** 
 * 类描述:人类接口  
 * @version 1.0  CreateDate: 2015-2-15
 *
 * @updateDate            
 * @updatePerson        
 * @declare 
 */
public interface IPerson {
    
    /**
     * 方法描述:自我介绍
     */
    public void selfIntrouction();
}

二:客户端实现类

package dmode.adapter;

/** 
 * 类描述:  中国人
 * @version 1.0  CreateDate: 2015-2-15
 *
 * @updateDate            
 * @updatePerson        
 * @declare 
 */
public class ChinaPerson implements IPerson{
    
    public ChinaPerson(){
    }
    
    public ChinaPerson(String name_x,String name_m){
        this.name_x = name_x;
        this.name_m = name_m;
    }
    
    /**
     * 中国人的姓
     */
    private String name_x;
    
    /**
     * 中国人的名
     */
    private String name_m;

    /**
     * @see dmode.adapter.IPerson#selfIntrouction()
     */
    @Override
    public void selfIntrouction() {
        System.out.println("你好,我叫" + name_x + name_m);
    }

    /**
     * @param name_x the name_x to set
     */
    public void setName_x(String name_x) {
        this.name_x = name_x;
    }

    /**
     * @param name_m the name_m to set
     */
    public void setName_m(String name_m) {
        this.name_m = name_m;
    }

}

三 第三方类(需要被适配的对象)

package dmode.adapter;

/** 
 * 类描述:  美国人
 * @version 1.0  CreateDate: 2015-2-15
 *
 * @updateDate            
 * @updatePerson        
 * @declare 
 */
public class AmerPerson  {
    
    public AmerPerson(){
        
    }
    
    public AmerPerson(String name_first,String name_last){
        this.name_first = name_first;
        this.name_last = name_last;
    }
    
    /**
     * 美国人的名字
     */
    private String name_first;
    
    /**
     * 美国人的姓氏
     */
    private String name_last;

    /**
     * 方法描述:美国人自我介绍
     * @param person
     */
    public void showSelf() {
        System.out.println("hello,I am" + name_first + "." + name_last);
    }

    /**
     * @return the name_first
     */
    public String getName_first() {
        return name_first;
    }

    /**
     * @return the name_last
     */
    public String getName_last() {
        return name_last;
    }
}

四:适配器(此处为对象适配器,通过聚合,类适配器需要继承第三方类)

package dmode.adapter;

/** 
 * 类描述:  人员适配器
 * @version 1.0  CreateDate: 2015-2-15
 *
 * @updateDate            
 * @updatePerson        
 * @declare 
 */
public class AdapterPerson implements IPerson{
    
    private AmerPerson amerPerson;
    
    public AdapterPerson(AmerPerson _amerPerson){
        this.amerPerson = _amerPerson;
    }

    /**
     * @see dmode.adapter.IPerson#selfIntrouction()
     */
    @Override
    public void selfIntrouction() {
        System.out.println("你好,我叫" + amerPerson.getName_last() + amerPerson.getName_first());
    }
    
}

五:应用场景

package dmode.adapter;

import java.util.ArrayList;
import java.util.List;

/** 
 * 类描述: 场景 ,人员做自我介绍
 * @version 1.0  CreateDate: 2015-2-15
 *
 * @updateDate            
 * @updatePerson        
 * @declare 
 */
public class Client {
    
    public static void main(String[] args) {
        
        //美国式的自我介绍
        AmerPerson amerPerson = new AmerPerson("迈克尔","乔丹");
        amerPerson.showSelf();
        
        //将美国人变成适配器人进行中国式的自我介绍
        List<IPerson> persons = new ArrayList<IPerson>();
        //IPerson chinaPerson = new ChinaPerson("张","三");
        IPerson person = new AdapterPerson(amerPerson);
        persons.add(person);
        for(IPerson per : persons){
            per.selfIntrouction();
        }
    }
}

结果:

hello,I am迈克尔.乔丹
你好,我叫乔丹迈克尔

设计模式之--适配器模式(Adapter)

标签:

原文地址:http://www.cnblogs.com/leonkobe/p/4292935.html

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