Design Pattern Explained 读书笔记四——Adapter

What?

Convert the interface of a class into another interface
that the clients expect. Adapter lets classes work together
that could not otherwise because of incompatible inter-
faces. ——GOF

不改变客户端代码,通过使用适配器去包装、兼容一个客户端无法直接兼容的接口.
一般分为:类适配器 与 对象适配器

UML图(对象适配器)

How(对象适配器)

先来个例子:
比如我接到需求要开发一套图形系统,于是我设计
统一接口(为了多态性)Shape,
Shape接口的行为是: display()

其中有一些实现类如,Triangle等等

/**
 * 这个接口是暴漏给客户端的,我们希望使用这个接口
 * 
 * Target
 */
public interface Shape {
    void display();

    void drawing();
}
/*
 * Target 实现类
 */
public class Triangle implements Shape {

    @Override
    public void display() {
        System.out.println("Triangle display");

    }

    @Override
    public void drawing() {
        System.out.println("Triangle drawing");

    }

}

当我在设计Circle这个对象时,发现小王已经写过了类似实现,并且逻辑完全一致,那我当然要复用了。小王的代码:

/**
 * 不兼容的接口
 * 
 * Adaptee :需要被适配的接口
 */
public interface Graph {
    void displayGraph();

    void drawingGraph();
}

这个Circle是我希望兼容的!

/**
 * 
 * Adaptee :需要被适配的接口
 */
public class Circle implements Graph {

    @Override
    public void displayGraph() {
        System.out.println("Circle displayGraph");

    }

    @Override
    public void drawingGraph() {
        System.out.println("Circle drowingGraph");

    }

}

但是,他的接口、方法参数各种不一致,难道我要改动?!怎么面向客户端用?于是我想使用Adapter,来复用小王的实现,同时满足我的代码设计结构,让客户端不该写代码直接使用。

/**
 * CircleAdapter包装了不兼容的Graph接口,并以Shape的形式暴漏给客户端
 */
public class CircleAdapter implements Shape {

    private Graph circle;// 组合的方式 不兼容的Circle
    //使用组合的好处是,任何Graph的子类都和以在适配器中使用

    public CircleAdapter(Graph circle) {
        this.circle = circle;
    }

    @Override
    public void display() {
        circle.displayGraph();
    }

    @Override
    public void drawing() {
        circle.drawingGraph();
    }

}

/**
 * 客户端面向(Target)Shape接口编程,由于Graph的实现类Circle中有我们可以复用的逻辑,
 * 
 * 所以我们想把这个Circle转化为Shape, 以兼容客户端使用
 *
 */
public class ClientToShape {
    public static void main(String[] args) {

        Shape triangle = new Triangle();
        testShape(triangle);

        Shape circleAdapter = new CircleAdapter(new Circle());
        testShape(circleAdapter);

    }

    static void testShape(Shape shape) {
        shape.display();
        shape.drawing();
    }
}

客户端向Shape发送请求,得到返回。完全没有觉察到是Adapter起了转换作用,把请求发到被适配者上并得到返回。
target:Shape
适配器:CircleAdapter;
被适配对象:(Graph)Circle
这里写图片描述

可以想一想,如果我们的目标是Graph,怎么让面向Graph定义实现的客户端使用Triangle呢?

类适配器

这里写图片描述

标准的UML图是需要多继承的,但是java不支持多继承,严格意义上并不适用,但是targe可以是个interface,算是扩展吧

当我们使用类适配器时,代码:


/**
 *
 * 使用继承的方式将不兼容的Circle引入
 */
public class CircleClassAdapter extends Circle implements Shape {

    @Override
    public void display() {
        super.displayGraph();
    }

    @Override
    public void drawing() {
        super.displayGraph();
    }

}

Adapter与Facade的比较:

相同:都是将老的系统包装起来。都是在新接口中复用老的系统代码。
不同:Adapter要做的是将一个错误的已经存在的接口转化为目前新的接口,我调新的接口也同样做老的事情(而且代码复用)。Facade纯粹是通过组合单纯的整合、复用老的系统代码。

总之:
Facade简化系统,而Adapter是一个已有接口转化为另一个新接口。

If you have ever bought any programming books, you might have noticed that there are two types of them: books that are too short to understand the topic and books that are too long making it inevitable that you get bored. 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 not tied to any specific programming language and will be good for you as long as you have a basic knowledge of OOP. Most chapters are available on the website, so you can check out the simplicity of the language in the book and the way materials are presented. Why should I read this book? It's simple. It's written in clear and simple language that makes it easy to read and understand. It's short. Yes, there are no useless demos or huge code listings — just clear and easy-to-understand descriptions with many graphical examples. When you finish reading this book, you'll have good reason to go to your boss and ask him for apromotion. Why? Because using design patterns will allow you to get your tasks done twice as fast, to write better code and to create efficient and reliable software architecture. How do I become a programming ninja? The main difference between a ninja and a novice is the knowledge of secret coding tricks, as well as the awareness of most pitfalls and the ability to avoid them. Design patterns were created as a bible for avoiding problems related to software design. Doesn’t that make it a true ninja’s handbook? Table of Contents Creational patterns Abstract Factory Builder Factory Method Object Pool Prototype Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Null Object Observer State Strategy Template Method Visitor
设计模式是一种解决常见问题的重复使用的方法。它们是在软件开发中创建可复用代码的指导方针。 设计模式有三个主要组成部分:模式名称、问题与解决方案的说明和实现该模式的示例代码。 模式名称是一个简短的描述,它简洁地概括了该模式的作用和用途。这有助于开发人员更好地理解和记忆该模式。 问题与解决方案的说明是设计模式的核心。它描述了一种常见问题的情况,并提供了一种有益的解决方案。通过遵循这种解决方案,开发人员可以更轻松地应对相似的问题。 示例代码是一种实现特定设计模式的具体代码。它向开发人员展示了如何在具体情况下应用该模式。 设计模式可以分为三类:创建型、结构型和行为型。 创建型设计模式涉及到对象的创建,帮助我们在创建对象时更加灵活和可扩展。包括工厂模式、单例模式和原型模式等。 结构型设计模式关注的是对象之间的关系,以及如何构建更大的结构。例如适配器模式、装饰器模式和代理模式等。 行为型设计模式描述了对象之间的通信和相互作用方式。这些模式能够帮助我们改善代码的灵活性和可维护性。包括策略模式、观察者模式和命令模式等。 总而言之,设计模式是软件开发中一种常用的方法,它以简洁明了的方式描述了常见问题的解决方案,并提供了示例代码以帮助开发人员更好地理解和应用这些解决方案。设计模式可以使代码更具可读性、可维护性和可扩展性,从而提高软件开发的效率和质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值