前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >四巨头23种设计模式的意图

四巨头23种设计模式的意图

作者头像
程序员吾真本
发布2018-08-20 11:34:40
3660
发布2018-08-20 11:34:40
举报
文章被收录于专栏:程序员吾真本程序员吾真本

了解设计模式的意图,是在代码重构中浮现并识别设计模式的关键。 本文将四巨头在《设计模式》一书的23种设计模式的意图放在一个页面里,方便查阅。四巨头把这23种设计模式按照创建、结构、行为分为三类。

创建(1. 抽象工厂、2. 建造者、3. 工厂方法、4. 原型、5. 单例)

  1. 抽象工厂 Intent意图:Provide an interface for creatingfamilies of related or dependent objects without specifying their concreteclasses. 提供一个接口,来创建一些彼此相关或依赖的对象族,而无须指定其具体的类。

Abstract Factory抽象工厂

  1. 建造者 Intent意图:Separate the construction of acomplex object from its representation so that the same construction processcan create different representations. 将一个复杂对象的构造与其表现形式进行分离,使得同样的构造过程能创建出不同的表现形式。

Builder建造者

  1. 工厂方法 Intent意图:Define an interface for creatingan object, but let subclasses decide which class to instantiate. Factory Methodlets a class defer instantiation to subclasses. 定义一个用于创建一个对象的接口,但让子类来决定具体实例化哪个类。工厂方法模式能让一个类将实例化的工作延迟到各个子类中来进行。

Factory Method工厂方法

  1. 原型 Intent意图:Specify the kinds of objects tocreate using a prototypical instance, and create new objects by copying thisprototype. 使用一个原型实例来指定所要创建的对象的类型,并通过复制该原型来创建新的对象。

Prototype原型

  1. 单例 Intent意图:Ensure a class only has oneinstance, and provide a global point of access to it. 确保一个类仅有一个实例,并提供一个对其进行访问的全局访问点。

Singleton单例


结构(1. 适配器、2. 桥接、3. 组合、4. 装饰器、5. 门面、6. 轻量级、7. 代理)

  1. 适配器 Intent意图:Convert the interface of a classinto another interface clients expect. Adapter lets classes work together thatcouldn't otherwise because of incompatible interfaces. 将一个类的接口转换成其客户端所期望的另一个接口。适配器模式能让以前因为接口不兼容而无法协同工作的一些类可以在一起工作。

Adapter适配器

  1. 桥接 Intent意图:Decouple an abstraction from its implementation so that the two can vary independently. 将一个抽象与其实现解耦合,使得两者能够独立地发生改变。

Bridge桥接

  1. 组合 Intent意图:Compose objects into treestructures to represent part-whole hierarchies. Composite lets clients treatindividual objects and compositions of objects uniformly. 将若干对象组合成树的结构,来表示“部分-整体”的层次结构。组合模式让客户端能统一地对待单个对象和对象的复合体。

Composite组合

  1. 装饰器 Intent意图:Attach additionalresponsibilities to an object dynamically. Decorators provide a flexiblealternative to subclassing for extending functionality. 为一个对象动态地附加额外的职责。除了子类化这个方案以外,装饰器模式为扩展代码功能提供了另一个灵活的备选方案。

Decorator装饰器

  1. 门面 Intent意图:Provide a unified interface to aset of interfaces in a subsystem. Facade defines a higher-level interface thatmakes the subsystem easier to use. 为一个子系统中的一系列接口提供一个统一的接口。门面模式定义了一个更高层次的接口,而令该子系统使用起来更加容易。

Facade门面

  1. 轻量级 Intent意图:Use sharing to support largenumbers of fine-grained objects efficiently. 使用共享来有效地支持巨大数量的细粒度的对象的使用。

Flyweight轻量级

  1. 代理 Intent意图:Provide a surrogate orplaceholder for another object to control access to it. 为另一个对象提供一个代理或占位者,来控制对该对象的访问。

Proxy代理


行为(1. 责任链、2. 命令、3. 翻译器、4. 迭代器、5. 中介者、6. 纪念品、7. 观察者、8. 状态、9. 策略、10. 模版方法、11. 访问者)

  1. 责任链 Intent意图:Avoid coupling the sender of arequest to its receiver by giving more than one object a chance to handle therequest. Chain the receiving objects and pass the request along the chain untilan object handles it. 通过让多个对象各自有一次机会来处理一个请求的方式,来避免将一个请求的发送者与其接受者相耦合。将各个接收该请求的对象链接起来,并将该请求沿着这个链条传递下去,直到有一个对象处理了该请求为止。

Chain of Responsibility责任链

  1. 命令 Intent意图:Encapsulate a request as anobject, thereby letting you parameterize clients with different requests, queueor log requests, and support undoable operations. 以一个对象的形式封装一个请求,从而能用不同请求来对客户端进行参数化,将请求进行排队或记录日志,并支持可撤销的操作。

Command命令

  1. 翻译器 Intent意图:Given a language, define arepresention for its grammar along with an interpreter that uses therepresentation to interpret sentences in the language. 给定一种语言,针对其语法定义一个表示形式,并提供一个能使用该表示形式的翻译器,来翻译这种语言的语句。

Interpreter翻译者

  1. 迭代器 Intent意图:Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 提供一种方法来顺序访问一个聚合对象的各个元素,而无须暴露该对象的底层表示。

Iterator迭代器

  1. 中介者 Intent意图:Define an object thatencapsulates how a set of objects interact. Mediator promotes loose coupling bykeeping objects from referring to each other explicitly, and it lets you varytheir interaction independently. 定义了一个对象,来将一系列对象之间如何进行交互进行封装。中介者模式能通过让这些对象免于显式地相互进行引用而促进松耦合,并能够做到独立地去改变这些对象之间的交互。

Mediator中介者

  1. 纪念品 Intent意图:Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later. 在不破坏封装的前提下,将一个对象的内部状态进行捕获并外部化,使得该对象能够在以后被恢复到这个内部状态。

Memento纪念品

  1. 观察者 Intent意图:Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 在若干对象之间定义一个一对多的依赖关系,使得当一个对象改变状态时,其所有的依赖者都被通知并被自动更新。

Observer观察者

  1. 状态 Intent意图:Allow an object to alter itsbehavior when its internal state changes. The object will appear to change itsclass. 当一个对象的内部状态改变时,允许该对象相应地改变其行为。这样该对象看起来好像改变了其所属的类。

State状态

  1. 策略 Intent意图:Define a family of algorithms,encapsulate each one, and make them interchangeable. Strategy lets thealgorithm vary independently from clients that use it. 定义一个算法族,对每一个算法进行封装,并令其能够实现互换。策略模式能让算法独立于那些使用它的客户端而发生变化。

Strategy策略

  1. 模版方法 Intent意图:Define the skeleton of analgorithm in an operation, deferring some steps to subclasses. Template Methodlets subclasses redefine certain steps of an algorithm without changing thealgorithm's structure. 定义一个操作中的算法的骨架,并将其中一些步骤延迟到若干子类中去执行。模板方法模式能让这些子类重新定义一个算法的某些步骤,而无须改变该算法的结构。

Template Method模板方法

  1. 访问者 Intent意图:Represent an operation to beperformed on the elements of an object structure. Visitor lets you define a newoperation without changing the classes of the elements on which it operates. 用来表示即将要施加到一个对象结构的各个元素之上的一个操作。访问者模式能用来定义一个新的操作,而无须改变其所操作的上述各个元素所属的类。

Visitor访问者

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.01.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 创建(1. 抽象工厂、2. 建造者、3. 工厂方法、4. 原型、5. 单例)
  • 结构(1. 适配器、2. 桥接、3. 组合、4. 装饰器、5. 门面、6. 轻量级、7. 代理)
  • 行为(1. 责任链、2. 命令、3. 翻译器、4. 迭代器、5. 中介者、6. 纪念品、7. 观察者、8. 状态、9. 策略、10. 模版方法、11. 访问者)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档