适配器模式理解

适配器模式 

适配器模式(别名:包装器)

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

Adapter Pattern(Another Name: Wrapper) 

    Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible【不兼容的】



 interfaces. 


一 、 概述



适配器模式是将一个类的接口被适配者转换成客户希望的另外一个接口(目标)的成熟模式,该模式中涉及有目标、被适配者和适配器。适配器模式的关键是建立一个适配器,这个适配器实现了目标接口并包含有被适配者的引用



实例 



用户已有一个两相的插座,但最近用户又有了一个新的三相插座。用户现在已经有一台洗衣机和一台电视机,洗衣机按着三相插座的标准配有三相插头,而电视机按着两相插座的标准配有两相插头。现在用户想用新的三相插座来使用洗衣机和电视机。


二、适配器模式的结构与使用 



模式的结构中包括三种角色:

 目标(Target) 


 被适配者(Adaptee) 


 适配器(Adapter) 




《适配器模式理解》

分析uml图

Adapter与Adaptee是关联关系,Adapter持有Adaptee的引用,并且泛化了Target 在methodA的方法体中,调用的是Adaptee的methodB的方法

1.目标(Target) : ThreeElectricOutlet.java 

public interface ThreeElectricOutlet{

    public abstract void connectElectricCurrent(); 

}

2.被适配者(Adaptee): TwoElectricOutlet.java 

public interface TwoElectricOutlet{

    public abstract void connectElectricCurrent(); 

 } 

3.适配器(Adapter) TreeElectricAdapter.java 

public class TreeElectricAdapter implements ThreeElectricOutlet{

   TwoElectricOutlet outlet;

   TreeElectricAdapter(TwoElectricOutlet outlet){

       this.outlet=outlet;

   }

   public void connectElectricCurrent(){

       outlet.connectElectricCurrent();

   }

}

4.应用 Application.java_1

 public class Application{

    public static void main(String args[]){

       ThreeElectricOutlet outlet;      

       Wash wash=new Wash();            

       outlet=wash;                      

       System.out.println(“使用三相插座接通电流:”);

       outlet.connectElectricCurrent();   

       TV tv=new TV();                    

       TreeElectricAdapter adapter=new TreeElectricAdapter(tv);

       outlet=adapter;                   

       System.out.println(“使用三相插座接通电流:”);

       outlet.connectElectricCurrent();   

    }

}

4.应用 Application.java_2

 class Wash implements ThreeElectricOutlet{ 

    String name;

    Wash(){

       name=”黄河洗衣机”;

    }

    Wash(String s){

       name=s;

    }

    public void connectElectricCurrent(){

       turnOn();

    }

    public void turnOn(){

       System.out.println(name+”开始洗衣物。”);

    }

}

4.应用 Application.java_3

 class TV implements TwoElectricOutlet{  

    String name;

    TV(){

       name=”长江电视机”;

    }

    TV(String s){

       name=s;

    }

    public void connectElectricCurrent(){

       turnOn();

    }

    public void turnOn(){

       System.out.println(name+”开始播放节目。”);

    }

}

三、适配器模式的优点 

目标(Target)和被适配者(Adaptee)是完全解耦的关系。

适配器模式满足“开-闭原则”。当添加一个实现Adaptee接口的新类时,不必修改Adapter,Adapter就能对这个新类的实例进行适配。

四.常见应用


java.io.InputStreamReader



 package java.io; import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import sun.nio.cs.StreamDecoder; public class InputStreamReader extends Reader { private final StreamDecoder sd; public InputStreamReader(InputStream in) { super(in); try { sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object } catch (UnsupportedEncodingException e) { // The default encoding should always be available throw new Error(e); } } public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException { super(in); if (charsetName == null) throw new NullPointerException("charsetName"); sd = StreamDecoder.forInputStreamReader(in, this, charsetName); } public InputStreamReader(InputStream in, Charset cs) { super(in); if (cs == null) throw new NullPointerException("charset"); sd = StreamDecoder.forInputStreamReader(in, this, cs); } public InputStreamReader(InputStream in, CharsetDecoder dec) { super(in); if (dec == null) throw new NullPointerException("charset decoder"); sd = StreamDecoder.forInputStreamReader(in, this, dec); } public String getEncoding() { return sd.getEncoding(); } public int read() throws IOException { return sd.read(); } public int read(char cbuf[], int offset, int length) throws IOException { return sd.read(cbuf, offset, length); } public boolean ready() throws IOException { return sd.ready(); } public void close() throws IOException { sd.close(); } } 



看这段代码

 sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object

后面调用的都是sd的方法其实,简单理解就是sd是一个中间的Adaptee对象,而这个类本身就是一个Adapter

    原文作者:MarioFei
    原文地址: https://blog.csdn.net/feipeixuan/article/details/23550173
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞