现在的位置: 首页 > 综合 > 正文

java设计模式6:Adapter

2017年09月22日 ⁄ 综合 ⁄ 共 3343字 ⁄ 字号 评论关闭
文章目录

结构模式有下面这些:适配器模式,缺省适配模式,合成模式,装饰模式,代理模式,享元模式,门面模式,桥接模式等.

适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。如下图所示,左边是类的适配器模式,右边是对象的适配器模式。

image

类的适配器模式把被适配的类的API转换成为目标类的API,其静态结构图如下图所示:

image

在上图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是继承关系,这决定了这个适配器模式是类的。

角色:
1、目标(Target):这就是所期待得到的接口。
2、源(Adaptee):现有需要适配的接口。
3、适配器(Adapter):适配器类是本模式的核心。适配器把源接口转换成目标接口。

代码:

image

package com.javapatterns.adapter.classAdapter;

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("方法名       "+Thread.currentThread().getStackTrace()[1].getMethodName());
        System.out.println("类名    "+Thread.currentThread().getStackTrace()[1].getClassName());
        System.out.println("文件名   " + Thread.currentThread().getStackTrace()[1].getFileName());
        System.out.println("所在的行数 "+Thread.currentThread().getStackTrace()[1].getLineNumber());
    }
}

 

 

package com.javapatterns.adapter.classAdapter;

public interface Target {
    /**
     * Class Adaptee contains operation sampleOperation1.
     */
    void sampleOperation1();

    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    void sampleOperation2();
}

 

package com.javapatterns.adapter.classAdapter;

public class Adapter extends Adaptee implements Target {
    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    public void sampleOperation2(){
        // Write your code here
        sampleOperation1();
    }
}

 

 

/**
* 测试类适配器模式
*/
package com.javapatterns.adapter.classAdapter;

/**
* @author luhx
*
*/
public class client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Adapter a= new Adapter();
        a.sampleOperation2();
    }

}

 

 

对象的适配器模式

对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类。

image 

从图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而此包装类能够把Adaptee的API与Target类的API衔接起来。Adapter 与Adaptee是委派关系,这决定了这个适配器模式是对象的。

源代码结构图:

image

package com.javapatterns.adapter.objectAdapter;

/**
* @author luhx
*
*/
public class client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Adapter a = new Adapter(new Adaptee());
        a.sampleOperation2();
    }

}

 

package com.javapatterns.adapter.objectAdapter;

public class Adapter implements Target {
public Adapter(Adaptee adaptee){
        super();
        this.adaptee = adaptee;
    }

    public void sampleOperation1(){

    }

    public void sampleOperation2(){
        // Write your code here
        adaptee.sampleOperation1();
    }

    /**
     * @uml.property  name="adaptee"
     * @uml.associationEnd  multiplicity="(1 1)"
     */
    private Adaptee adaptee;
}

 

 

package com.javapatterns.adapter.objectAdapter;

public class Adaptee {
    public void sampleOperation1(){
        System.out.println("方法名       "+Thread.currentThread().getStackTrace()[1].getMethodName());
        System.out.println("类名    "+Thread.currentThread().getStackTrace()[1].getClassName());
        System.out.println("文件名   " + Thread.currentThread().getStackTrace()[1].getFileName());
        System.out.println("所在的行数 "+Thread.currentThread().getStackTrace()[1].getLineNumber());

    }
}

 

 

package com.javapatterns.adapter.objectAdapter;

public interface Target {
    /**
     * Class Adaptee contains operation sampleOperation1.
     */
    void sampleOperation1();

    /**
     * Class Adaptee doesn't contain operation sampleOperation2.
     */
    void sampleOperation2();
}

抱歉!评论已关闭.