四个(GoF)设计模式
时间:2020-02-23 14:41:21 来源:igfitidea点击:
四种设计模式的帮派是《设计模式:可重用的面向对象软件的元素》一书中的23种设计模式的集合。
这本书于1994年首次出版,是学习设计模式的最受欢迎的书之一。
该书由Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides撰写。
由于四位作者,它被昵称为"四个设计模式的帮派"。
此外,它的简称为" GoF设计模式"。
GoF设计模式类型
GoF设计模式分为三类:
创作性:处理对象创建的设计模式。
结构:此类别中的设计模式涉及类结构,例如继承和组成。
行为的:这种类型的设计模式提供了更好的解决方案,以实现对象之间的更好交互,如何提供丢失耦合以及将来轻松扩展的灵活性。
创作设计模式
创新设计模式类别中有5个设计模式。
Pattern Name | Description |
---|---|
Singleton | The singleton pattern restricts the initialization of a class to ensure that only one instance of the class can be created. |
Factory | The factory pattern takes out the responsibility of instantiating a object from the class to a Factory class. |
Abstract Factory | Allows us to create a Factory for factory classes. |
Builder | Creating an object step by step and a method to finally get the object instance. |
Prototype | Creating a new object instance from another similar instance and then modify according to our requirements. |
结构设计模式
《四大帮派设计模式》一书中定义了7种结构设计模式。
Pattern Name | Description |
---|---|
Adapter | Provides an interface between two unrelated entities so that they can work together. |
Composite | Used when we have to implement a part-whole hierarchy. For example, a diagram made of other pieces such as circle, square, triangle, etc. |
Proxy | Provide a surrogate or placeholder for another object to control access to it. |
Flyweight | Caching and reusing object instances, used with immutable objects. For example, string pool. |
Facade | Creating a wrapper interfaces on top of existing interfaces to help client applications. |
Bridge | The bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client program. |
Decorator | The decorator design pattern is used to modify the functionality of an object at runtime. |
行为设计模式
GoF设计模式中定义了11种行为设计模式。
Pattern Name | Description |
---|---|
Template Method | used to create a template method stub and defer some of the steps of implementation to the subclasses. |
Mediator | used to provide a centralized communication medium between different objects in a system. |
Chain of Responsibility | used to achieve loose coupling in software design where a request from the client is passed to a chain of objects to process them. |
Observer | useful when you are interested in the state of an object and want to get notified whenever there is any change. |
Strategy | Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime. |
Command | Command Pattern is used to implement lose coupling in a request-response model. |
State | State design pattern is used when an Object change it’s behavior based on it’s internal state. |
Interpreter | defines a grammatical representation for a language and provides an interpreter to deal with this grammar. |
Iterator | used to provide a standard way to traverse through a group of Objects. |
Memento | The memento design pattern is used when we want to save the state of an object so that we can restore later on. |