`
pouyang
  • 浏览: 313402 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns 结构模式 之 Adapter 模式

阅读更多
Design Patterns  结构模式 之 Adapter 模式
定义
将一个类的接口转换成客户希望的另外一个接口。Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作



1 首先客户需要调用这个类,从这个类中去调用Adaptee类输出A
  public class Target {
	public void request() {
		System.out.println("Target");
	}
}


2 Adaptee(需要被适配的类)
public class Adaptee 
	public void request() {	
		System.out.print("A");
	}
}


问题来了,Target是无法访问到Adaptee这个类的,这两个类无法一起工作,加入适配器就可以工作了

3 Adapter(适配器)
public class Adapter extends Target {
	Adaptee adaptee = null;
	public Adapter(Adaptee adaptee) {
		this.adaptee = adaptee;
	}
	public void  request() {
		adaptee.request();
	}
}


由于Adapter 继承了 客户调用的类 Target
客户调用代码
public class Client {     
  public static void main(String[] args) {   
           
	  Target target = new Adapter(new Adaptee());
	  target.request();//在这个中调用之前无法一起工作的Adaptee
  }     
} 


适配器对要被调用的类进行了一个包装。
分享到:
评论

相关推荐

    .NET Design Patterns [Kindle Edition]

    After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, ...

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍《Head First Design Patterns》的用法;第1章到第11章陆续介绍的设计模式为Strategy、...

    Design.Patterns.Explained.Simply

    We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...

    Design Patterns in Modern C++--2018

    Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply ...

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers ■Chapter 1: Overview of SOLID Principles and Design Patterns ■Chapter 2: SOLID Principles ■Chapter 3: Creational Patterns: ...

    Design Patterns Elements of Reusable Object-Oriented Software

    • How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Programming.in.the.Large.with.Design.Patterns

    It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...

    Head First Design Patterns

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、 State、Proxy。最后三章比较...

    Head First Design Patterns 高清英文版

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    Head First Design Patterns 英文原版

    Using the latest research in neurobiology, cognitive science, and learning theory, Head First Design Patterns will load patterns into your brain in a way that sticks. In a way that lets you put them ...

    Packt.Go.Design.Patterns.2017

    Structural Patterns - Composite, Adapter, and Bridge Design Patterns Chapter 4. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns Chapter 5. Behavioral Patterns - Strategy...

    Data Structures And Algorithms With Object-Oriented Design Patterns In C Sharp.pdf

    the emerging object-oriented design patterns. Experienced object-oriented programmers find that certain ways of doing things work best and that these ways occur over and over again. The book shows how...

    Head First Design Patterns(英文,无水印,完整版)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、State、Proxy。*后三章比较...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    design_patterns_in_typescript:TypeScript中的设计模式实现

    结构模式 行为模式 编译项目 $ git clone https://github.com/torokmark/design_patterns_in_typescript.git $ cd design_patterns_in_typescript $ tsc 根目录中有一个tsconfig.json文件,负责编译器选项。 设置后...

Global site tag (gtag.js) - Google Analytics