`

读《研磨设计模式》-代码笔记-适配器模式-Adapter

阅读更多
声明:
本文只为方便我个人查阅和理解,详细的分析以及源代码请移步 原作者的博客http://chjavach.iteye.com/


package design.pattern;

/*
 * 适配器模式解决的主要问题是,现有的方法接口与客户要求的方法接口不一致
 * 可以这样想,我们要写这样一个类(Adapter):
 * 1.这个类要符合客户的要求 ---> 那显然要implements客户要求的接口
 * 2.这些接口的实现是调用已有的类 ---> 有两种方法可获得已有的类的方法:
 * a.组合。对应“对象适配器”
 * b.继承。对应“类适配器”
 * 
 * 书上讲了一个新旧版日志操作的例子(旧版日志是存储在文件里,新版是存储在数据库里)
 * 书上说“新旧版日志共存”,但我觉得主要解决的问题不是共存(书上也不关注新版日志操作如何实现),
 * 而是应该强调如何“用新版日志的接口方法去调用旧版”
 * 书上新版日志操作的一个方法,正是调用了旧版的方法来实现,而新版的实现需自行处理,不在讨论范围内:
 public void updateLog(LoginModel lm){
       List<LogModel> list = adaptee.readLogFile();		//旧版的方法,文件操作
       for(int i=0;i<list.size();i++){
           if(list.get(i).getLogId().equals(lm.getLogId())){
              list.set(i, lm);
              break;
           }
       }
       adaptee.writeLogFile(list);
 }
 
 书上最后作了一个扩展:双向适配器,也是很有实际意义的。还是以日志操作为例,要求:
 1、主要使用旧版接口,但使用旧版接口时也希望能调用新版接口
 2、新版接口能兼容旧版接口——这是前面的例子
 我的理解其实就是说适配器为这两个需求提供两个“视图”或者说两套接口
 视图1:
 1.正常使用旧接口的方法(例如这个方法叫method())
 2.调用适配器的方法,从而实现“也能调用新接口”: new Adapter().method();
 视图2:
 1.正常使用新接口的方法(例如这个方法叫method2())
 2.调用适配器的方法,从而实现“也兼容旧接口”: new Adapter().method2();
 对于适配器来说,这其实有点分裂:旧版方法method()里面的实现代码其实是新版的实现代码,method2()里面的实现代码其实是旧版的实现代码
*/


//客户要求的接口
interface Target {
	void request();
}


//现有的接口或方法。与客户要求不一致。这个“不一致”可能是多样的,不局限于“方法名不一致”
//这个Adaptee也可以是实现现有的某个接口
class Adaptee {
	public void specificRequest() {
		System.out.println("specificRequest in adaptee.");
	}
}


//对象适配器
class ObjectAdapter implements Target {

	//如果Adaptee是implements某个接口,那应该写成:
	//private IAdaptee adaptee;
	private Adaptee adaptee;

	public ObjectAdapter(Adaptee adaptee) {
		this.adaptee = adaptee;
	}

	//调用旧的方法
	public void request() {
		adaptee.specificRequest();
	}

}


//类适配器
class ClassAdapter extends Adaptee implements Target {
	public void request() {
		super.specificRequest();
	}
}


//扩展:双向适配器

interface IOldAdaptee {
	public void method();
}


interface INewAdaptee {
	public void method2();
}


class TwoWayAdapter implements IOldAdaptee, INewAdaptee {

	private IOldAdaptee oldAdaptee;
	private INewAdaptee newAdaptee;

	public void method() {
		newAdaptee.method2();
	}

	public void method2() {
		oldAdaptee.method();
	}
}


class OldAdaptee implements IOldAdaptee {

	public void method() {}
	
}


class NewAdaptee implements INewAdaptee {
	
	public void method2() {}
	
}


class OldClient {
	
	public void test(){
		IOldAdaptee adaptee = new OldAdaptee();
		adaptee.method();
		//现在客户想通过旧接口调用新版的方法,那就要有一个适配器
		IOldAdaptee adapter = new TwoWayAdapter();		//注意这里adapter在类型是IOldAdaptee,也就是提供的是旧版的”视图“
		adapter.method();		//适配器里面调用的是新版的方法
	}
	
}


class NewClient {
	
	public void test(){
		INewAdaptee adaptee = new NewAdaptee();
		adaptee.method2();
		//现在客户想通过新接口调用旧版的方法,那就要有一个适配器
		//INewAdaptee adapter = new TwoWayAdapter();
		adaptee = new TwoWayAdapter();
		adaptee.method2();		//适配器里面调用的是旧版的方法
	}
	
}


public class AdapterPattern {

	public static void main(String[] args) {
		Adaptee adaptee = new Adaptee();
		Target adapter = new ObjectAdapter(adaptee);
		adapter.request();
		
		Target adapter2 = new ClassAdapter();
		adapter2.request();

	}

}
1
7
分享到:
评论

相关推荐

    研磨设计模式-配套源代码

    研磨设计模式-配套源代码研磨设计模式-配套源代码

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.rar

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码.7z

    研磨设计模式-配套源代码 UTF-8格式

    研磨设计模式第二版-配套源代码 UTF-8格式 研磨设计模式第二版-配套源代码 UTF-8格式

    研磨设计模式-陈臣.epub

    “1.1 设计模式是什么 1.1.1 什么是模式 从字面上理解,模,就是模型、模板的意思;式,就是方式、方法的意思。综合起来,所谓模式就是:可以作为模型或模板的方式或方法。... “研磨设计模式”。 iBooks.

    研磨设计模式-陈臣.王斌.扫描高清版PDF

    设计模式(Design Pattern)是一套被反复使用、多数人知晓的、经过分类的、代码设计经验的总结。 使用设计模式的目的:为了代码可重用性、让代码更容易被他人理解、保证代码可靠性。 设计模式使代码编写真正工程化;...

    研磨设计模式-陈臣pdf

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式-陈臣.mobi kindle版

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式书-配套源代码(全)

    1:本源代码是《研磨设计模式》一书的配套源代码 2:每个模式的示例源代码放在一个单独的文件夹下,以该模式的英文名称命名 3:每个模式下分成多个example,按照书的示例顺序分别命名为example1、example2.........

    研磨设计模式-part2

    第4章 适配器模式(Adapter) 第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章...

    研磨设计模式-part4

    第4章 适配器模式(Adapter) 第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章...

    研磨设计模式-part3

    第4章 适配器模式(Adapter) 第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章...

    研磨设计模式--chjavach的博客文章

    单例模式、工厂方法模式、策略模式、命令模式和桥接模式。

    研磨设计模式源码

    研磨设计模式的配套源码,请下载,不要想了,解压后,直接放到eclipse下面即可运行!

    研磨设计模式 源代码

    研磨设计模式 源代码 书上面的例子都在里面

    研磨设计模式(完整带书签).part2.pdf

    第4章 适配器模式(Adapter) 第5章 单例模式(Singleton) 第6章 工厂方法模式(Factory Method) 第7章 抽象工厂模式(Abstract Factory) 第8章 生成器模式(Builder) 第9章 原型模式(Prototype) 第10章...

    研磨设计模式全部源代码

    研磨设计模式全部源代码,个人收集,仅用学习使用,不可用于商业用途,如有版权问题,请联系删除!

    研磨设计模式带书签完整版228M.7z.001

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式 完美书签 完整(一)

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

    研磨设计模式带书签完整版228M.7z.002

    《研磨设计模式》完整覆盖GoF讲述的23个设计模式并加以细细研磨。初级内容从基本讲起,包括每个模式的定义、功能、思路、结构、基本实现、运行调用顺序、基本应用示例等,让读者能系统、完整、准确地掌握每个模式,...

Global site tag (gtag.js) - Google Analytics