Java23种设计模式——深入理解对象适配器模式与抽象外观模式
来源:互联网 发布:阿里云买域名空间 编辑:程序博客网 时间:2024/05/29 11:21
该系列文章为博主学习笔记,原文请参考参考连接
本文禁止转载,但是欢迎大家留言区留言交流[微笑]
对象适配器模式
刚刚在思考觉得对象适配器模式和抽象外观模式觉得很像,都是一个客户端去调用一些子系统,通过一个接口或者一个抽象类屏蔽具体操做的流程。但是针对于客户端却又本质上的差别。
抽象外观模式:
这是上一篇中提到的抽象外观模式UMl图。
我们先截取一半,为了方便与后文的对象适配器模式进行比较。
适配器模式参考链接:参考连接
图我们先放在一边,我们来说一下对象适配器模式:
以参考链接举的列子继续探讨,当客户端已经针对一个接口进行编程,知道这个接口的一些方法。当想要调用这些方法的时候,却发现另一个类(我们称为适配者类)中已经有了具体的实现。然后我们在接口和适配者类之间,创建了一个适配器类,并且在该适配器类中保留对适配者类的引用。通过适配器类进行对适配者类方法的调用。
代码:
public interface Target { public void sort(); public void search();}
//适配器类public class AdapterTest implements Target { private Adaptee adaptee; public AdapterTest() { adaptee = new Adaptee(); } @Override public void sort() { adaptee.sort(); } @Override public void search() { adaptee.search(); }}
//适配器者类public class Adaptee { public void sort(){ System.out.println("Adaptee.sort"); } public void search(){ System.out.println("Adaptee.search"); }}
public class myClass { public static void main(String[] args) { Target target; target = new AdapterTest(); target.sort(); target.search(); }}
打印:
Adaptee.sort
Adaptee.search
对象适配器模式与抽象外观模式的差别
大家看到这里有没有发现对象适配器模式和抽象外观模式十分的相似
我们再来对比下两个模式的UMl流程图:
经过对比发现了惊人的相似,可以说就是同一个UMl的流程图,具体流程都为一下几部:
1、创建一个抽象类或者接口(抽象外观模式为:abstractFacade,对象适配器模式为:Targe)
2、创建一个实现类继承或者实现步骤1中的类(抽象外观模式为:Newfacade,对象适配器模式为:AdapterTest)
3、在实现类中持有另一个类的引用(抽象外观模式的实现类Newfacade持有了InitInternetTitle对象的引用,对象适配器模式的实现类AdapterTest持有了Adaptee对象的引用)
经过UML流程图和我上面分析的三个步骤,大家可以看到对象适配器模式和抽象外观模式没有什么本质的区别,最起码在中间过程的实现代码都是一样的。
经过我的深入分析发现有一点不一样,那就是客户端类的实现以及调用不一样
我们先来回顾一下抽象外观模式客户端都做了什么:
public class myClass { public static void main(String[] args){ AbstractFacade abstractFacade; abstractFacade = new NewFacade(); abstractFacade.doSomething(); }}
抽象外观模式的客户端,只是通过了抽象类事例了对象并且调用了doSomething方法,而这个方法里面都做了什么,客户端根本不知道,完全对用户屏蔽了。
再来看看对象适配器模式的客户端做了什么:
public class myClass { public static void main(String[] args) { Target target; target = new AdapterTest(); target.sort(); target.search(); }}
通过了抽象类事例了对象,但是唯一的区别就是客户端知道自己要做什么,没有对用户屏蔽具体做的业务,但是具体的业务可能不是自己实现的而是通过一个接口与其他类的其他方法相连接,这就是对象适配器模式。
类适配器模式
类适配器模式也很简单,只不过将在适配器类中引用适配者类对象改为适配器类继承适配者类。
具体代码:
public class AdapterTest extends Adaptee implements Target { private Adaptee adaptee; public AdapterTest() { adaptee = new Adaptee(); } @Override public void sort() { super.sort(); } @Override public void search() { super.search(); }}
但是由于Java语言的特点,只允许单一继承,如果Target不是一个接口而是一个类,或者如果适配者类Adaptee是一个final类,final类不允许继承,我们将无法使用类适配器模式。
双向适配器模式
双向适配器模式对我的理解就相当于一个适配器存在多个适配者,适配者可以通过它调用目标类中的方法,目标类也可以通过它调用适配者类中的方法。
代码:
public interface Adaptee { public void specificRequest();}
public interface Target { public void request();}
public class Adaptertest implements Target, Adaptee { private Target target; private Adaptee adaptee; public Adaptertest(Target target) { this.target = target; } public Adaptertest(Adaptee adaptee) { this.adaptee = adaptee; } public void request() { adaptee.specificRequest(); } public void specificRequest() { target.request(); }}
public class ConcreteAdaptee implements Adaptee{ @Override public void specificRequest() { System.out.println("ConcreteAdaptee.specificRequest"); }}
public class ConcreteTarget implements Target{ @Override public void request() { System.out.println("ConcreteTarget.request"); }}
public class myClass { public static void main(String[] args) { Target target=new Adaptertest(new ConcreteAdaptee()); target.request(); Adaptee adaptee=new Adaptertest(new ConcreteTarget()); adaptee.specificRequest(); }}
输出:
ConcreteAdaptee.specificRequest
ConcreteTarget.request
最后优缺点引用下原文的吧:原文
这里在仔细说一下对象适配器的缺点如何通过代码更改
比如我们要置换ConcreteAdaptee类中的specificRequest方法,我们首先创建一个AfterAdaptee类并且继承ConcreteAdaptee类,并且重写需要被置换的方法。
public class AfterAdaptee extends ConcreteAdaptee { @Override public void specificRequest() { System.out.println("AfterAdaptee.specificRequest"); }}
然后在客户端机型稍微的更改即可:
public class myClass { public static void main(String[] args) { Target target=new Adaptertest(new AfterAdaptee()); target.request(); Adaptee adaptee=new Adaptertest(new ConcreteTarget()); adaptee.specificRequest(); }}
打印:
AfterAdaptee.specificRequest
ConcreteTarget.request
微信公众号:
QQ交流群:365473065
- Java23种设计模式——深入理解对象适配器模式与抽象外观模式
- Java23种设计模式——外观模式
- 设计模式--适配器模式与外观模式
- 设计模式-适配器模式与外观模式
- 设计模式之适配器与外观模式
- Java23种设计模式--Adapter(适配器)
- Java23种设计模式--Adapter(适配器)
- 设计模式——适配器模式和外观模式
- 设计模式——适配器,桥接,外观
- java23种设计模式--外观模式(facade)
- 重学java23种设计模式(10)外观模式
- HeadFirst 设计模式笔记(七)—— 适配器模式与外观模式
- 适配器与外观模式
- java23种设计模式--适配器模式(Adapter)
- java23种常用设计模式之适配器模式(Adapter)
- 重学java23种设计模式(6)适配器模式
- java23种设计模式之适配器模式(Adapter)
- 适配器模式与外观模式
- 代理传递方法的作用
- 无题
- android实现自定义顶部标题栏
- Windows10下安装Ubuntu14.04双系统
- Https报错java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
- Java23种设计模式——深入理解对象适配器模式与抽象外观模式
- linux下的C语言开发(makefile编写)
- tomcat7.0线程设置参数的配置实例
- mongodb 提升性能
- C++11新特性学习笔记—变长参数的宏定义以及__VA_ARGS__
- leetcode Best Time to Buy and Sell Stock 系列
- The Proposal of Service Oriented Data Mining System for Solving Real-Life Classification--阅读笔记
- React调试工具,chrome React扩展+source-map
- PHP自动加载机制[1]---__autoload操作详解