Notes
  • Introduce
  • Go
    • Grammar
      • Basic
      • Goroutines & Channels
      • Test
    • System Library
      • Module
      • sync
      • context
      • net
    • Concurrency in Go
    • The Go Memory Model
    • Code Snippet
  • Rust
    • The Rust Programming Language
    • Rust by Example
  • JAVA
    • Preface
    • Grammar
      • Basic
      • Data Types
      • Operator
      • Exceptions
    • Class Libraries
      • Collection
      • Stream
      • IO
      • NIO
      • RMI
    • Concurrency
      • Preface
      • JMM
      • Synchronized & CAS
      • Deadlock
      • Thread
      • Lock & Condition
      • Utility Class
      • Thread-safe Collection
      • Atomic Class
      • Fork/Join
      • Concurrency Design Patterns
        • Immutable
        • Copy-on-Write
        • ThreadLocal
        • Multitheading If
        • Division
    • JVM
      • Class & Instance Initialization
      • Runtime Data Area
      • Garbage Collection
    • Web Container
      • Tomcat Architecture
      • Jetty Architecture
    • Spring
    • Tuning
      • Programming
  • Computer Science
    • Computer Organization
    • Algorithm
      • Complexity
      • Linear List
      • Sort
      • Binary Search
      • Skip List
      • Hash Table
      • Tree
      • Graph
      • String Matching
      • Bloom Filter
      • Greedy Algorithm
      • Divide and Conquer
      • Back Tracking
      • Dynamic Programming
    • Network Protocol
      • Pysical Layer
      • Data Link Layer
      • Network Layer
      • Transport Layer
      • Application layer
      • HTTP
      • HTTP/2 in Action
    • Operating System
      • Basic
      • System Initialization
      • Diagnostic Tools
      • CPU Diagnosis
      • Memory Diagnosis
      • Disk Diagnosis
      • Network Diagnosis
      • Monitor System
    • Design Patterns
      • UML
      • OOP
      • Principle
      • Refactoring & Specification
      • Creational
        • Singleton
        • Factory
        • Builder
        • Prototype
      • Structural
        • Proxy
        • Bridge
        • Decorator
        • Adapter
        • Facade
        • Composite
        • FlyWeight
      • Behavioral
        • Observer
        • Template Method
        • Strategy
        • State
        • Iterator
        • Chain of Responsibility
    • Distributed System
      • Protocol & Algorithm
      • Transcation
      • Theory
      • Resource Management
      • Scheduling
      • Computing
      • Message Queue
      • Cache
      • Consistent Hashing
  • database
    • InfluxDB
      • In-Memory Index
      • Meta
    • MySQL
      • SQL
      • Architecture
      • Log
      • Transaction
      • Indexing
      • Lock
      • Storage
    • Redis
    • Elasticsearch
      • Local Debug
    • HBase
    • Kafka
    • ZooKeeper
  • Reading
    • RocketMQ
    • 演说之禅
    • So Good They Can't Ignore You
    • 学会提问
    • Lecture
  • Other
    • v2ray
    • Kubernetes
    • Git
    • Maven
    • Anaconda And Conda
    • Fuck! Shit!
      • Remove Final by Reflection
      • Ingress Host
      • ExecuterService submit
  • Open source contribution
Powered by GitBook
On this page

Was this helpful?

  1. Computer Science
  2. Design Patterns
  3. Structural

Adapter

适配器模式将不兼容的接口转换为兼容的接口。

实现方式有类适配器和对象适配器两种,类适配器采用继承,对象适配器采用组合。两种实现有各自的场景:

  • 若 Adaptee 接口不多,两种都可以。

  • 若 Adaptee 接口较多,且与 ITarget 大部分相同,用类适配器比较合适。

  • 若 Adaptee 接口较多,且与 ITarget 大部分不同,则用对象适配器合适。

// 需要转换成的接口定义
public interface ITarget {
  void f1();
  void fc();
}

// 不兼容 ITarget 接口定义的接口
public class Adaptee {
  public void fa() { //... }
  public void fc() { //... }
}

// 类适配器: 基于继承
public class Adaptor extends Adaptee implements ITarget {
  public void f1() {
    super.fa();
  }
  
  // fc()不需要实现,直接继承自Adaptee,这是跟对象适配器最大的不同点
}

// 对象适配器:基于组合
public class Adaptor implements ITarget {
  private Adaptee adaptee;
  
  public Adaptor(Adaptee adaptee) {
    this.adaptee = adaptee;
  }
  
  public void f1() {
    adaptee.fa(); //委托给Adaptee
  }
  
  public void fc() {
    adaptee.fc();
  }
}

适配器模式的应用场景:

  • 封装有缺陷的接口设计。比如依赖的外部系统的接口有缺陷(比如包含大量静态方法),我们可以对接口进行二次封装,抽象出更好的接口。

  • 统一多个类的接口设计。系统若依赖多个外部系统,通过适配器模式,将他们的接口适配为统一个接口定义。比如依赖多个敏感词过滤器,需要轮流使用。

  • 替换依赖的外部系统。比如阿里云的 oss 和华为云的 obs。

  • 兼容老版本接口。比如 JDK1.0 有Enumeration 类,JDK2.0 开发更好的 Iterator 类,此时不能直接删除 Enumeration 类,因为项目中有很多类用到了这个,我们可以把 Enumeration 的实现改为 iterator。

PreviousDecoratorNextFacade

Last updated 5 years ago

Was this helpful?