- 浏览: 932493 次
- 性别:
- 来自: 魔都
文章分类
- 全部博客 (745)
- MultiThread (19)
- My Plan (118)
- JavaBasic (61)
- MyInterview (104)
- InternetTechnique (5)
- ProjectConclusion (1)
- Maven (5)
- MogoDb (5)
- Hadoop (11)
- Memcached (6)
- TechniqueCollect (1)
- Ibaits (1)
- Android (34)
- ItLife (40)
- Tree (2)
- ProjectArchitect (7)
- Open Source (3)
- liunx (5)
- socket (8)
- Spring (27)
- DesginPattern (35)
- WebBasic (13)
- English (13)
- structs (1)
- structs2 (2)
- Oracle (17)
- Hibernate (2)
- JavaScript (4)
- Jdbc (1)
- Jvm (15)
- Ibatis (1)
- DataStructures (13)
- Https/Socket/Tcp/Ip (3)
- Linux (4)
- Webservice (7)
- Io (2)
- Svn (1)
- Css (1)
- Ajax (1)
- ExtJs (1)
- UML (2)
- DataBase (6)
- BankTechnique (3)
- SpringMvc (3)
- Nio (3)
- Load Balancing/Cluster (3)
- Tools (1)
- javaPerformanceOptimization (8)
- Lucene(SEO) (1)
- My Think (80)
- NodeJs (1)
- Quartz (1)
- Distributed-java (1)
- MySql (7)
- Project (4)
- junit (4)
- framework (1)
- enCache (1)
- git (2)
- SCJP (1)
- sd (1)
最新评论
-
lkjxshi:
你都这水平了还考这个证干嘛
SCJP 认证考试指南 -
钟逸华:
问的真多
百度java开发面试题(转) -
zuimeitulip:
觉得我就是这样的,从小阅读量就很少,导致现在的读的速度非常慢, ...
让读书成为一种习惯 -
DDT_123456:
我觉得你是不符合要求。问你hashmap的那个问题,你那样回答 ...
阿里面试2(转) -
jingjing0907:
刚刚写了很多读过此博客的感受,竟然没有发上去,以为我注册账号还 ...
让读书成为一种习惯
文章链接:http://blog.csdn.net/surprisesdu/article/details/606148
适配器模式(Adapter Pattern)[GOF95]把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
问题:
我们已经在应用程序中设计了接口sampleOperation1, 然后我们又为了加快开发而购买了可以很好的完成此功能的第三方库, 然而第三方库的接口sampleOperation2和我们设计的接口sampleOperation1不同, 为了使这些接口不兼容的类可以一起工作, 我们应用Adapter模式将第三方库的接口sampleOperation2转换为我们的接口sampleOperation1 (也是客户希望的接口).
适配器模式有两种形式.
一、 类的适配器模式 类图: 源代码: package com.designpatterns.adapter; /** * 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口 * @author suki */ public interface Target { public void sampleOperation1(); } /** * 源角色,相当于第三方库 * @author suki */ public class Adaptee { public Adaptee() { } public void sampleOperation2() { System.out.println("Adaptee.sampleOperation2()"); } } /** * 适配器角色 * 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1 * * @author suki */ public class Adapter extends Adaptee implements Target { public void sampleOperation1() { this.sampleOperation2(); } } /** * 客户端类 * @author suki */ public class Client { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(); target.sampleOperation1(); } } 源代码: package com.designpatterns.adapter; /** * 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口 * @author suki */ public interface Target { public void sampleOperation1(); } /** * 源角色,相当于第三方库 * @author suki */ public class Adaptee { public Adaptee() { } public void sampleOperation2() { System.out.println("Adaptee.sampleOperation2()"); } } /** * 适配器角色 * 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1 * * @author suki */ public class Adapter extends Adaptee implements Target { public void sampleOperation1() { this.sampleOperation2(); } } /** * 客户端类 * @author suki */ public class Client { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(); target.sampleOperation1(); } } 二、 对象的适配器模式 在类的适配器模式中Adapter采用继承的方式复用Adaptee的接口,而在对象的适配器模式中Adapter则采用组合的方式实现Adaptee的复用 Interface Target +sampleOperation1:void Adaptee +sampleOperation2:void Adapter -adaptee:Adaptee +Adapter +sampleOperation1:void 类图: 源代码: package com.designpatterns.adapterofobject; /** * 目标角色, 相当于我们的应用程序, sampleOperation1是我们开放给客户的接口 * @author suki */ public interface Target { public void sampleOperation1(); } /** * 源角色,相当于第三方库 * @author suki */ public class Adaptee { public Adaptee() { } public void sampleOperation2() { System.out.println("Adaptee.sampleOperation2()"); } } /** * 适配器角色 * 把第三方库的接口sampleOperation2转化为我们开放给客户的接口sampleOperation1 * @author suki */ public class Adapter implements Target { private Adaptee adaptee; public Adapter(Adaptee adaptee) { super(); this.adaptee = adaptee; } public void sampleOperation1() { adaptee.sampleOperation2(); } } /** * 客户端类 * @author suki */ public class Client { public static void main(String[] args) { Adaptee adaptee = new Adaptee(); Target target = new Adapter(adaptee); target.sampleOperation1(); } }
发表评论
-
关于Java 23种设计模式的有趣见解
2013-08-10 18:01 982关于Java 23种设计模式的有趣见解 [来源] 51CT ... -
Java中常用的设计模式(转)
2013-01-30 12:30 1076下面是我对一些常用设计模式的理解: 模式分为:行为模 ... -
工厂模式,抽象工厂模式,简单工厂的区别
2012-10-10 21:35 9281.抽象工厂模式与工厂方法模式的最大区别就在于,工 ... -
java设计模式之常用设计模式分析
2012-06-05 09:09 1237常用的设计模式有:工厂模式(工厂方法模式,简单工厂模式, ... -
java设计模式之责任链模式(行为模式 )
2012-06-04 14:31 1080(11).责任链模式: 文章链接:http:// ... -
java设计模式之观察着模式(行为模式)
2012-06-04 14:28 951(10).观察着模式Observer 文章链接:ht ... -
java设计模式之适配器模式(结构型模式)
2012-06-04 14:26 644(9).适配器模式 文章链接:http://chjl2 ... -
java设计模式之动态代理模式(结构型模式)
2012-06-04 14:24 1122(8).代理模式: 文章 ... -
java设计模式之代理模式(结构型模式)
2012-06-04 14:22 1019(8).代理模式: ... -
java设计模式之——装饰模式结(构型模式)
2012-06-03 19:34 968三.例子代码:使用装饰 ... -
java设计模式之缺省适配器模式(构型模式)
2012-06-03 19:31 1348缺省适配器模式:文章链接:http://jzinfo.itey ... -
java设计模式之模板方法模式(行为模式 )
2012-06-03 19:03 1082package com.createtype.desginpa ... -
java设计模式之——组合模式(结构型模式 )
2012-06-03 18:24 1298接口的作用是什么?定 ... -
java设计模式之——策略模式(行为模式 )
2012-06-03 16:54 1021文章链接:http://yangguangfu ... -
java设计模式之简单工厂模式(创建型模式)
2012-06-01 00:01 1230/** * ## 具体产品(Con ... -
工厂模式,简单工厂模式,抽象工厂模式三者有什么区别
2012-05-31 23:59 1260文章链接: http://blog.csdn.net/ ... -
java设计模式之抽象工厂模式(创建型模式)
2012-05-31 23:57 1104package com.createtype.desgi ... -
java设计模式之工厂方法模式(创建型模式)
2012-05-31 23:56 914package com.createtype.desg ... -
java设计模式之单例模式(创建型模式)
2012-05-30 17:48 1032单列模式: 优点 ... -
java中设计模式分类
2012-05-01 18:31 1115常用的设计模式有:工厂模式(工厂方法模式,简单工厂模式, ...
相关推荐
设计模式之适配器模式(Adapter Pattern) 将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。
Head First 设计模式 (七) 适配器模式(Adapter pattern) C++实现
8、适配器模式ADAPTER PATTERN 9、模板方法模式TEMPLATE METHOD PATTERN 10、建造者模式BUILDER PATTERN 11、桥梁模式BRIDGE PATTERN 12、命令模式COMMAND PATTERN 13、装饰模式DECORATOR PATTERN 14、迭代器模式...
- **Adapter(适配器)**:这是实现目标接口并含有适配者实例的类。适配器的作用是将适配者的接口转换为目标接口。 2. **工作原理**: - 适配器模式通过引入一个中间层即适配器来解决两个不同接口之间的兼容性问题...
适配器模式(Adapter Pattern) 是作为两个不兼容接口之间的桥梁, 这种类型的设计模式属于结构型模式。 一些书籍也称适配器模式为缺省适配器模式(Default Adapter Pattern) 。 适配器模式主要分 为三类: 类...
NULL 博文链接:https://lym6520.iteye.com/blog/713094
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个...
.NET设计模式(8):适配器模式(AdapterPattern)
java设计模式之适配器模式,希望对大家有所帮助。
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个...
用c++方法实现AdapterPattern模式
本文实例讲述了Python设计模式之...适配器模式(Adapter Pattern):将一个类的接口转换成为客户希望的另外一个接口.Adapter Pattern使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 应用场景:系统数据和
适配器模式(Adapter Pattern) 提供者模式(Provider Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) 原型模式(Prototype Pattern) 责任链模式(Chain of Responsibility Pattern) 中介者模式...
适配器模式[ADAPTER PATTERN] 模板方法模式[TEMPLATE METHOD PATTERN] 建造者模式[BUILDER PATTERN] 策略模式 代理模式 单例模式 多例模式 工厂方法模式 抽象工厂模式 门面模式 适配器模式 模板方法模式 建造者模式...
适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 过滤器模式(Filter、Criteria Pattern) 组合模式(Composite Pattern) 装饰器模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式...
适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. 代理模式...
7. 适配器模式 (Adapter Pattern) 8. 模板方法模式 (Template Pattern) 9. 建造者模式 (Builder Pattern) 10. 原型模式 (Prototype Pattern) 11. 组合模式 (Composite Pattern) 12. 装饰者模式 ...
适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式(Facade Pattern) 11. 享元模式(Flyweight Pattern) 12. ...
适配器模式(Adapter Pattern) 桥接模式(Bridge Pattern) 组合模式(Composite Pattern) 装饰者模式(Decorator Pattern) 外观模式(Facade Pattern) 享元模式(Flyweight Pattern) ...