四个(GoF)设计模式

时间:2020-02-23 14:41:21  来源:igfitidea点击:

四种设计模式的帮派是《设计模式:可重用的面向对象软件的元素》一书中的23种设计模式的集合。

这本书于1994年首次出版,是学习设计模式的最受欢迎的书之一。
该书由Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides撰写。
由于四位作者,它被昵称为"四个设计模式的帮派"。
此外,它的简称为" GoF设计模式"。

GoF设计模式类型

GoF设计模式分为三类:

  • 创作性:处理对象创建的设计模式。

  • 结构:此类别中的设计模式涉及类结构,例如继承和组成。

  • 行为的:这种类型的设计模式提供了更好的解决方案,以实现对象之间的更好交互,如何提供丢失耦合以及将来轻松扩展的灵活性。

创作设计模式

创新设计模式类别中有5个设计模式。

Pattern NameDescription
SingletonThe singleton pattern restricts the initialization of a class to ensure that only one instance of the class can be created.
FactoryThe factory pattern takes out the responsibility of instantiating a object from the class to a Factory class.
Abstract FactoryAllows us to create a Factory for factory classes.
BuilderCreating an object step by step and a method to finally get the object instance.
PrototypeCreating a new object instance from another similar instance and then modify according to our requirements.

结构设计模式

《四大帮派设计模式》一书中定义了7种结构设计模式。

Pattern NameDescription
AdapterProvides an interface between two unrelated entities so that they can work together.
CompositeUsed when we have to implement a part-whole hierarchy. For example, a diagram made of other pieces such as circle, square, triangle, etc.
ProxyProvide a surrogate or placeholder for another object to control access to it.
FlyweightCaching and reusing object instances, used with immutable objects. For example, string pool.
FacadeCreating a wrapper interfaces on top of existing interfaces to help client applications.
BridgeThe bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client program.
DecoratorThe decorator design pattern is used to modify the functionality of an object at runtime.

行为设计模式

GoF设计模式中定义了11种行为设计模式。

Pattern NameDescription
Template Methodused to create a template method stub and defer some of the steps of implementation to the subclasses.
Mediatorused to provide a centralized communication medium between different objects in a system.
Chain of Responsibilityused to achieve loose coupling in software design where a request from the client is passed to a chain of objects to process them.
Observeruseful when you are interested in the state of an object and want to get notified whenever there is any change.
StrategyStrategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.
CommandCommand Pattern is used to implement lose coupling in a request-response model.
StateState design pattern is used when an Object change it’s behavior based on it’s internal state.
Interpreterdefines a grammatical representation for a language and provides an interpreter to deal with this grammar.
Iteratorused to provide a standard way to traverse through a group of Objects.
MementoThe memento design pattern is used when we want to save the state of an object so that we can restore later on.