Proxy, Decorator, Adapter and Bridge Patterns – 代理人、装饰者、适配器和桥接模式

最后修改: 2017年 9月 23日

中文/混合/英文(键盘快捷键:t)

 

1. Introduction

1.介绍

In this article, we’re going to focus on Structural Design Patterns in Java – and discuss what these are and some fundamental differences between some of them.

在这篇文章中,我们将专注于Java中的结构性设计模式–并讨论这些模式是什么,以及其中一些模式之间的一些基本区别。

2. Structural Design Patterns

2.结构化设计模式

According to the Gang Of Four (GoF), design patterns can be classified into three types:

根据四人帮(Gang Of Four,GoF)的说法,设计模式可以分为三种类型。

  1. Creational
  2. Structural
  3. Behavioral

Simply put, Structural Patterns deal with the composition of classes and objects. They provide different ways of using object composition and inheritance to create some abstraction.

简单地说,结构模式处理的是类和对象的组合。它们提供了使用对象组合和继承的不同方式来创建一些抽象。

3. Proxy Pattern

3.代理模式

With this pattern, we create an intermediary that acts as an interface to another resource, e.g., a file, a connection. This secondary access provides a surrogate for the real component and protects it from the underlying complexity.

通过这种模式,我们创建了一个中介,作为另一个资源的接口,例如,一个文件、一个连接。这种二级访问为真正的组件提供了一个代理,并保护其不受底层复杂性的影响。

For a detailed example of the pattern, have a look at the dedicated post: The Proxy Pattern in Java.

关于该模式的详细例子,请看专门的帖子。Java中的代理模式

Key Points of Differentiation:

差异化的关键点:

  • The proxy provides the same interface as the object it’s holding the reference to, and it doesn’t modify the data in any manner; it’s in contrast to Adapter and Decorator patterns which alter and decorate the functionalities of pre-existing instances respectively
  • The Proxy usually has the information about the real subject at the compile time itself whereas Decorator and Adapter get injected at runtime, knowing only the actual object’s interface

4. Decorator Pattern

4.装饰者模式

This pattern is useful for enhancing the behavior of an object. For a detailed overview, have a look at the focused tutorial here: Decorator Pattern in Java

这种模式对于增强对象的行为非常有用。关于详细的概述,请看这里的重点教程。Java中的装饰器模式

Key points of differentiation:

差异化的关键点:

  • Although Proxy and Decorator patterns have similar structures, they differ in intention; while Proxy’s prime purpose is to facilitate ease of use or controlled access, a Decorator attaches additional responsibilities
  • Both Proxy and Adapter patterns hold a reference to the original object
  • All the decorators from this pattern can be used recursively, an infinite number of times, which is not possible with other models

5. Adapter Pattern

5.适配器模式

The Adapter pattern is used for connecting two incompatible interfaces that otherwise cannot be connected directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the interface needed.

适配器模式用于连接两个不兼容的接口,否则无法直接连接。适配器用一个新的接口来包装一个现有的类,使其与所需的接口兼容。

For a detailed description and implementation, have a look at the dedicated post: Adapter Pattern in Java

关于详细的描述和实现,请看专门的帖子。Java中的Adapter模式</a

The main differences between Adapter and Proxy patterns are:

Adapter和Proxy模式的主要区别是:

  • While proxy provides the same interface, Adapter provides a different interface that’s compatible with its client
  • Adapter pattern is used after the application components are designed so that we can use them without modifying the source code. This is in contrast to the Bridge pattern, which is used before the components are designed.

6. Bridge Pattern

6.桥形图案

The Bridge pattern is used to decouple an abstraction from its implementation so that the two can vary independently.

桥接模式用于将一个抽象概念与其实现解耦,以便两者可以独立变化。

This means to create a bridge interface that uses OOP principles to separate out responsibilities into different abstract classes.

这意味着要创建一个桥梁接口,使用OOP原则将责任分离到不同的抽象类中。

For a detailed description and implementation, have a look at the dedicated post: Bridge Pattern in Java

关于详细的描述和实现,请看专门的帖子。Java中的桥模式

Key Points of Differentiation:

差异化的关键点

  • A Bridge pattern can only be implemented before the application is designed.
  • Allows an abstraction and implementation to change independently whereas an Adapter pattern makes it possible for incompatible classes to work together

7. Conclusion

7.结论

In this article, we focused on the Structural Design Pattern and differences between some of its types.

在这篇文章中,我们重点讨论了结构化设计模式和它的一些类型之间的区别。

As always, the full implementation of this tutorial can be found over on Github.

一如既往,本教程的完整实现可以在Github上找到over