您的位置:首页 > 其它

设计模式 - 适配器模式,装饰模式,代理模式,外观模式(一)

2017-04-01 17:06 513 查看

概述

适配器模式(adapter pattern),装饰模式(decorator pattern),代理模式(proxy pattern)和外观模式( facade pattern),都是设计模式中结构型设计模式,而且他们都是对类行为的封装,很容易让人迷惑,因此,放在一起讲解对比,以便让我们更清楚这四种设计模式的特点和实践应用。

适配器模式

先看wiki上定义:

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Interfaces may be incompatible, but the inner functionality should suit the need. The Adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

适配器让两个不兼容的接口一起工作。这是真实世界适配器的定义。接口可能不兼容,但是内部功能必须满足需求。适配器设计模式通过将一个类的接口转换为客户端期待的接口来使不兼容类之间一起工作。

UML图如下:



代理模式

概念:

a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic. In the proxy extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.

代理模式是被客户端调用来访问场景后面的真实服务的对象。代理的使用可以是简单的forward(面向对象中是指使用一个对象的元素可以访问到其他对象的相应元素)到真实的对象,或者提供额外的逻辑。比如缓存,先决条件检查,对客户端来说,使用代理和使用真实对象时一样的,因为他们的接口相同。

UML图如下:



装饰模式

概念:

the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern.

装饰模式允许通过静态或者动态方式给单独的对象增加额外的行为,而不影响类的其他对象的行为。装饰模式经常用于遵守“单一职责原则”,因为它允许将类之间令人担忧的地方(不想关心)分开。即装饰类封装的类的功能细节我们不需要知道

UML图如下:



外观模式

概念:

The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client. It typically involves a single wrapper class that contains a set of members required by client.

当一个系统非常复杂或者难以理解时外观模式经常被使用,由于系统有大量相互依赖的类或者源代码不可用。该模式隐藏了细节并提供一个简单的接口给客户端。一般包含一个持有客户端需要的成员的集合的封装类。

UML图如下:



对比

经常上面的介绍,我们可以感受到,这四种设计模式,都是对一种行为的封装,只是侧重点和应用场合稍微不同。从他们的名字和介绍可以看下他们的侧重点:

adapter pattern

当我们必须遵守一个接口或者支持多种形态的行为时,强调兼容型和多形式

decorator pattern

当我们在运行时需要添加或者修改接口的行为时, 强调对原始对象行为的修改

facade pattern

当我们想给基础的对象一个简单的的接口时,强调封装提供简单接口

proxy pattern

当我们想对对象接口有些校验或者处理时,强调相同接口但是代理了额外的功能

使用感悟

在实际工作中,一般当我需要对接口进行条件检查,异常判断等辅助功能时,就使用Proxy模式,情况是即使不使用Proxy模式,我们也可以直接调用接口,只是需要额外处理些逻辑,因此选择proxy模式封装是一个好的选择。

当一个类提供了一个类型的接口,但是我客户端期待换一个更通用的接口,或者更符合客户端的接口时,比如获取连接池中的连接,我仅仅想用getConnection拿到mysql的连接,但是mysql的一些基础库提供了它自己的获取连接方式,我会选择adapter模式。或者我需要多种通用的接口形式时,我会用adapter模式。

当一个类提供了核心功能,但我想对这些核心功能做更多希望的操作时候,我会使用decorator模式。

当一个类处理流程非常复杂,提供的接口非常多,非常基础,而不想让客户端了解很多,只提供最简单需要的接口是,我会使用facade模式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐