java设计模式6:Adapter
来源:互联网 发布:mastercam铣螺纹编程 编辑:程序博客网 时间:2024/05/17 23:03
源地址: http://blog.csdn.net/luhouxiang/article/details/6542570
结构模式有下面这些:适配器模式,缺省适配模式,合成模式,装饰模式,代理模式,享元模式,门面模式,桥接模式等.
适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。如下图所示,左边是类的适配器模式,右边是对象的适配器模式。
类的适配器模式把被适配的类的API转换成为目标类的API,其静态结构图如下图所示:
在上图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是继承关系,这决定了这个适配器模式是类的。
角色:
1、目标(Target):这就是所期待得到的接口。
2、源(Adaptee):现有需要适配的接口。
3、适配器(Adapter):适配器类是本模式的核心。适配器把源接口转换成目标接口。
代码:
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类。
从图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而此包装类能够把Adaptee的API与Target类的API衔接起来。Adapter 与Adaptee是委派关系,这决定了这个适配器模式是对象的。
源代码结构图:
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();
}
- java设计模式6:Adapter
- java设计模式6:Adapter
- JAVA设计模式-Adapter
- Java设计模式:Adapter(适配器)
- java设计模式之Adapter
- java设计模式6——适配器模式(Adapter)
- JAVA设计模式(6) —<结构型>适配器模式(Adapter)
- java设计模式6--适配器模式(Adapter )
- (6)Java设计模式-适配器模式(Adapter)
- Java设计模式-----Adapter适配器模式
- java 设计模式之一 适配器模式 adapter
- java设计模式---Adapter适配器模式
- Java设计模式 - Adapter(适配器模式)
- Java设计模式-----Adapter适配器模式
- 学习:java设计模式—Adapter模式
- Java:设计模式之适配器模式Adapter
- 学习:java设计模式—Adapter模式
- Java-设计模式(Adapter适配器模式)
- 泛型和注解
- opencv-SURF和STFT
- 使用jQuery和CSS将背景图片拉伸
- 单例模式
- git版本控制使用入门
- java设计模式6:Adapter
- uva 10318 - Security Panel
- C#委托详解
- 千与千寻django(五)---django的自带管理界面
- 原型制作
- 高手真经 13条Java核心技术
- 虚拟机中linux无法访问外网
- C++ 标准头文件与C头文件区别与联系
- PHP包含或读取了哪个配置文件的跟踪探讨。