GOF设计模式二: 适配器模式 Adapter
   为中国市场生产的电器,到了美国,需要有一个转接器才能使用墙上的插座,这个转接 器的功能、原理?
复习单实例模式

   SingleTon的三个关键点

     1)私有的:构造函数

     2)私有的:成员变量,记录这个单实例

     3)公有的getter函数:没有实例时创建它;已有实例则返回 该实例
   面向对象中的适配器 Object oriented adapters

     Scenario: you have an existing software system that you need to work a new vendor library

       into, but the new vendor designed their interfaces differently than the last vendor
   What to do? Write a class that adapts the new vendor interface into the one you’re expecting.
    The adapter implements the interface your classes expect
    And talks to the vendor interface to service your requests
3.3 适配器模式 The Adapter Pattern Defined

  The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter l

    ets classes work together that couldn’t otherwise because of incompatible interfaces

    将一个类的接口转换成客户希望的另外一个接口, Adapter模式使原本由于接口不兼容而不能一起工作的那些类可以一起工作
  Full of good OO design principles:多个好的OO设计原则

    --Use of object composition 对象组合

    --Pattern binds the client to an interface and not an implementation 客户面向接口而不是实现
3.5 The Adapter Pattern: Key Features
  Intent

    Match an existing object beyond your control to a particular interface
  Problem

    A system has the right data and behavior but the wrong interface. Typically used when you have to make something a derivative of

      an abstract class
  Solution

    The Adapter provides a wrapper with the desired interface
  Participants and collaborators
    The Adapter adapts the interface of an Adaptee to match that of the Adapter's Target (the class it derives from). This allows

      the Client to use the Adaptee as if it were a type of Target
  Consequences

    The pattern allows for preexisting objects to fit into new class structures without being limited by their interfaces
  Implementation

    Contain the existing class in another class. Have the containing class match the required interface and call the methods of the

      contained class
3.6 实现细节 Check list

     两个角色 Identify the players

       Adaptee:The Adapter adapts the interface of an Adaptee to match that of the Adapter‘s Target (the class it derives from)

       Client:use the Adaptee as if it were a type of Target

     标识客户需要用到的接口 Identify the interface that the client requires

     设计一个“包装器”使得 Adaptee 符合客户的需要

     adapter/wrapper 类里面必须要有一个 adaptee class的对象 (负责工作)

     adapter/wrapper 负责“映射” 客户接口与 adaptee 的接口

     client uses (is coupled to) the new interface
3.7 适配器小结 Summary

   当现有的系统需要使用另一个类Adaptee的功能,而那个类的接 口又不符合现有的系统,就要使用适配器

    When you need to use an existing class and its interface is not the one you need, use an adapter

   适配器负责把Adaptee类的接口转换成客户类需要的格式,以完 成客户的工作

    An adapter changes an interface into one a client expects

   实现一个适配器的工作量取决于目标接口的规模

    Implementing an adapter may require little work or a great deal of work depending on the size and complexity of the target interface

8.3 GOF设计模式二: 适配器模式 Adapter的更多相关文章

  1. 设计模式二 适配器模式 adapter

    适配器模式的目的:如果用户需要使用某个类的服务,而这项服务是这个类用一个不同的接口提供的,那么,可以使用适配器模式为客户提供一个期望的接口.

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

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

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

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

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

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

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

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

  6. JavaScript设计模式 Item9 --适配器模式Adapter

    适配器模式(转换器面模式),通常是为要使用的接口,不符本应用或本系统使用,而需引入的中间适配层类或对象的情况. 适配器模式的作用是解决两个软件实体间的接口不兼容的问题. 一.定义 适配器模式(Adap ...

  7. Java设计模式之适配器模式(Adapter)

    转载:<JAVA与模式>之适配器模式 这个总结的挺好的,为了加深印象,我自己再尝试总结一下 1.定义: 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法 ...

  8. python 设计模式之适配器模式 Adapter Class/Object Pattern

    #写在前面 看完了<妙味>和<华医>,又情不自禁的找小说看,点开了推荐里面随机弹出的<暗恋.橘生淮南>,翻了下里面的评论,有个读者从里面摘了一段自己很喜欢的话出来, ...

  9. [设计模式] 7 适配器模式 adapter

    在 Adapter 模式的结构图中可以看到,类模式的 Adapter 采用继承的方式复用 Adaptee的接口,而在对象模式的 Adapter 中我们则采用组合的方式实现 Adaptee 的复用 类模 ...

随机推荐

  1. 《高性能Mysql》解读---Mysql的事务和多版本并发

    1.base:ACID属性,并发控制 2.MySql事务的隔离级别有哪些,含义是什么? 3.锁知多少,读锁,写锁,排他锁,共享锁,间隙锁,乐观锁,悲观锁. 4.Mysql的事务与锁有什么关联?MySq ...

  2. 【数据结构】算法 LinkList (Add Two Numbers)

    两个用链表代表的整数,其中每个节点包含一个数字.数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头.写出一个函数将两个整数相加,用链表形式返回和. Solution:建立一个新链表C,然 ...

  3. Dataguard配置总结

    Dataguard配置总结 本例情形 在主库存在运行的情况下,增加配置dataguard备库,实现双机热备,高可用性. 主库要求,归档模式,强制归档. 主库idty 备库idty_st 1.密码文件 ...

  4. 【SparkStreaming学习之三】 SparkStreaming和kafka整合

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk1.8 scala-2.10.4(依赖jdk1.8) spark ...

  5. 用python写栈

    class StackFullError(Exception): pass class StackEmptyError(Exception): pass class Stack: def __init ...

  6. MySQL 压缩文件安装遇到的问题及解决方案

    第一步:从官网下载压缩文件(链接). 第二步:解压该文件,放置到想放到的位置.我的目录是在 C:\mysql\mysql-8.0.12-winx64 下. 第三步:在C:\mysql\mysql-8. ...

  7. Path Analyzer Pro出现raw socket问题

    使用windows7,8以及10平台运行一个traceroute liketools软件,名为Path Analyzer Pro 2.7,遇到raw socket问题,如图: 原因是raw socke ...

  8. 为什么做java开发的公司需要那么多程序员?

    注:文章转载自知乎 透过现象看本质. Java是企业应用市场的王者,如果一家非互联网公司用Java,那么十有八九是做企业应用的. 所以,这个问题本质上是:为什么做企业应用的公司需要那么多Java程序员 ...

  9. sai u 2016

    再过20分钟,我就要结束2016年的工作回家过春节了.真是难过的一天啊,从来没有今天那么感受深刻,那么嫌弃时间太慢,没有归家心似箭,没有近乡情怯,只是好想,呵呵,来个午睡,来场电影,来点小说,哈哈哈. ...

  10. asp.net(c#)网页跳转 方法小结

    返回 打印 asp.net(c#)网页跳转七种方法小结_实用技巧_脚本之家 在asp.net下,经常需要页面的跳转,下面是具体的几种方法.跳转页面是大部编辑语言中都会有的,正面我们来分别介绍一下关于. ...