适配器模式(Adapter Pattern)

推荐一个很全面的博客:https://blog.csdn.net/zxt0601/article/details/52848004

定义:将一个类的接口转换成客户希望的另外一个接口,核心是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)。

何时使用(摘抄自runnoob):1、系统需要使用现有的类,而此类的接口不符合系统的需要。2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)

类图:二次整理时画

后期整理(springMVC中的适配器模式,真是巧妙~):https://www.cnblogs.com/tongkey/p/7919401.html

代码(这里采用一个很经典的demo):

1、现有的一个220V的电源:

package com.pat.Aadapter;
/**
* 这是一个220V的电源
* @author ZX
*
*/
public class PowerSource220V {
protected int v = 220;
//输出方法,输出220v
public int outPut() {
return v;
}
}

2、110V的接口,持有转换方法:

package com.pat.Aadapter;
/**
* 110V的接口,持有一个转换方法
* @author ZX
*
*/
public interface PowerSource110V {
//转换
int convert();
}

3、适配器对象:

package com.pat.Aadapter;
/**
* 适配器,继承了220v电源,并且实现了110v中的转换方法
* @author ZX
*
*/
public class Adapter extends PowerSource220V implements PowerSource110V{ @Override
public int convert() {
//调用父类方法
int outPutPower = outPut();
int convertPower =outPutPower/2;
return convertPower; } }

4、测试:

package com.pat.Aadapter;

public class Test {
public static void main(String[] args) {
//220V电源
PowerSource220V ps220 = new PowerSource220V();
int outPut = ps220.outPut();
System.out.println(outPut);
//使用适配器
Adapter ada = new Adapter();
int adapterOutPut = ada.convert();
System.out.println(adapterOutPut);
}
}

5:结果:

220
110

======================================================================

追加一个委托,JDK源码中非常常用:

1、随便找的一个类:

Executors.newCachedThreadPool(threadFactory);

2、点进去:

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}

3、下一层:

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory) {
this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
threadFactory, defaultHandler);
}

4、最终的构造方法:

public ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue,
ThreadFactory threadFactory,
RejectedExecutionHandler handler) {
if (corePoolSize < 0 ||
maximumPoolSize <= 0 ||
maximumPoolSize < corePoolSize ||
keepAliveTime < 0)
throw new IllegalArgumentException();
if (workQueue == null || threadFactory == null || handler == null)
throw new NullPointerException();
this.acc = System.getSecurityManager() == null ?
null :
AccessController.getContext();
this.corePoolSize = corePoolSize;
this.maximumPoolSize = maximumPoolSize;
this.workQueue = workQueue;
this.keepAliveTime = unit.toNanos(keepAliveTime);
this.threadFactory = threadFactory;
this.handler = handler;
}

5、这是我很喜欢的一种方式,面向需求变更编程啊哈哈。

================================================================

所有的源代码链接:

【设计模式】【最后一个】结构型07适配器模式(Adapter Pattern)的更多相关文章

  1. 《精通Python设计模式》学习结构型之适配器模式

    大名鼎鼎~~ 在兼容老系统和其它系统外调用时,用得着~ class Synthesizer: def __init__(self, name): self.name = name def __str_ ...

  2. 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)

    怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...

  3. Java经典设计模式之七大结构型模式

    转载: Java经典设计模式之七大结构型模式 博主在大三的时候有上过设计模式这一门课,但是当时很多都基本没有听懂,重点是也没有细听,因为觉得没什么卵用,硬是要搞那么复杂干嘛.因此设计模式建议工作半年以 ...

  4. 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调

    模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...

  5. 【设计模式】适配器模式 Adapter Pattern

    适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...

  6. 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)

    原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...

  7. 二十四种设计模式:适配器模式(Adapter Pattern)

    适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...

  8. 设计模式 - 适配器模式(adapter pattern) 具体解释

    适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...

  9. 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释

    适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...

随机推荐

  1. Entity Framework 6 编译出错的问题(VS2012)

    更新:其实这个问题是由于VS2012的EF代码生成模板是EF 5.x的,自然会与EF6 的runtime不兼容.起初我按照更新前的方式解决了,后来却发现会出现不止这一处命名空间发生改动而导致的问题. ...

  2. 分布式高级(十三)Docker Container之间的数据共享

    sudo docker run -it -v /usr/lib:/usr/lib/dbdata --name dbcontainer-192.168.1.184 ubuntu:14.04 sudo d ...

  3. WPF中用于嵌入其他进程窗口的自定义控件(AppContainer)

    原文:WPF中用于嵌入其他进程窗口的自定义控件(AppContainer) 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199 ...

  4. 2-21-源码编译安装LAMP

      编译安装LAMP所需要及其所使用的源码版本: httpd version:httpd-2.4.16 apr version:apr-1.5.2 pcre version:pcre-8.37 apr ...

  5. XF 主从页面

    using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...

  6. JS 小鸟飞

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  7. MVC 身份验证和异常处理过滤器

    :在Global中注册为全局过滤器,应用于所有的Controller的Action 参数类均继承自ControllerContext,主要包含属性请求上下文.路由数据.结果 using FilterE ...

  8. huawei 通过BGP的团体属性进行路由控制

    网络拓扑 XRV1的配置: =========================================================================== # sysname ...

  9. ADB 基础命令使用

    1.adb shell(>=2个设备显示:error: more than one device/emulator,仅连接一个设备可用) adb -d shell 只运行在真实设备中 adb - ...

  10. 通过Chrome扩展来批量复制知乎好友

        1.初始化文件 Chrome 官方扩展教程地址 新建一个文件夹 zhi-follow 下图中 1 部分为 默认的图标3种尺寸 会显示在 Chrome 中   2. 定义按钮样式   页面上会有 ...