设计模式--概念篇

对设计模式的概念进行一个整理,将设计模式按功能分类,方便查阅。算法

设计原则

  • 单一职责
  • 里式替换
  • 依赖倒置
  • 迪米特法则
  • 接口隔离
  • 开闭原则

单一职责

There should never be more than one reason for a class to change.设计模式

里式替换

  1. 子类必须彻底实现父类的方法
  2. 子类能够有本身的个性
  3. 覆盖或实现父类的方法时输入参数能够被放大
  4. 覆写或实现父类的方法时输出结果能够被缩小

依赖倒置

High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.数据结构

  1. 高层模块不该该依赖低层模块, 二者都应该依赖其抽象
  2. 抽象不该该依赖细节
  3. 细节应该依赖抽象

迪米特法则

迪米特法则也称为最少知识原则, 一个对象应该对其余对象有最少的了解。app

接口隔离

  1. 接口要尽可能小
  2. 接口要高内聚
  3. 定制化服务
  4. 接口 设计 是有 限度 的

开闭原则

Software entities like classes, modules and functions should be open for extension but closed for modifications.
一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。框架

设计模式

建立型

  • 单例模式
  • 工厂模式
  • 构建者模式
  • 原型模式

结构型

  • 适配器模式
  • 桥接模式
  • 过滤器模式
  • 组合模式
  • 装饰者模式
  • 门面模式
  • 享元模式
  • 代理模式

行为型

  • 责任链模式
  • 命令模式
  • 解释器模式
  • 迭代器模式
  • 中介者模式
  • 备忘录模式
  • 观察者模式
  • 状态模式
  • 空对象模式
  • 策略模式
  • 模版模式
  • 访问者模式

单例模式

定义

Ensure a class has only one instance, and provide a global point of access to it.
确保某一个类只有一个实例,并且自行实例化并向整个系统提供这个实例。ide

通用类图

工厂方法模式

定义

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
定义一个用于建立对象的接口,让子类决定实例化哪个 类。工厂方法使一个类的实例化延迟到其子类。函数

通用类图

抽象工厂模式

定义

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
为建立一组相关或相互依赖的对象提供一个接口,并且无须指定它们的具体类。flex

通用类图

模版方法模式

定义

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm' s structure.
定义一个操做中的算法的框架,而将一些步骤延迟到子类中。使得子类 能够不改变一个算法的结构便可重定义该算法的某些特定步骤。this

通用类图

建造者模式

定义

Separate the construction of a complex object from its representation so that the same construction process can create different representations.
将一个复杂对象的构建与它的表示分离,使得一样的构建过程能够 建立不一样的表示。prototype

通用类图

代理模式

定义

Provide a surrogate or placeholder for another object to control access to it.
为其余对象提供一种代理以控制对这个对象的访问。

通用类图

原型模式

定义

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
用原型实例指定建立对象的种类,而且经过拷贝这些原型建立新的对象。

通用类图

中介者模式

定义

Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
用一个中介对象封装一系列的对象交互,中介者使各对象不须要显示地相互做用,从而使其耦合松散,并且能够独立地改变它们之间的交互。

通用类图

命令模式

定义

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
将一个请求封装成一个对象,从而让你使用不一样的请求把客户端参数化,对请求排队或者记录请求日志,能够提供命令的撤销和恢复功能。

通用类图

责任链模式

定义

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。

通用类图

装饰者模式

定义

Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.
动态地给一个对象添加一些额外的职责。就增长功能来讲,装饰模式相比生成子类更为灵活。

通用类图

策略模式

定义

Define a family of algorithms, encapsulate each one, and make them interchangeable.
定义一组算法,将每一个算法都封装起来,而且使它们之间能够互换。

通用类图

适配器模式

定义

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn' t otherwise because of incompatible interfaces.
将一个类的接口变换成客户端所期待的另外一种接口,从而使本来因接口不匹配而没法在一块儿工做的两个类可以在一块儿工做。

通用类图

迭代器模式

定义

Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
它提供一种方法访问一个容器对象中各个元素,而又不需暴露该对象的内部细节。

通用类图

组合模式

定义

Compose objects into tree structures to represent part- whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
将对象组合成树形结构以表示“部分-总体”的层次结构,使得用户对单个对象和组合对象的使用具备一致性。

通用类图

观察者模式

定义

Define a one- to- many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则全部依赖于它的对象都会获得通知并被自动更新。

通用类图

门面模式

定义

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher- level interface that makes the subsystem easier to use.
要求一个子系统的外部与其内部的通讯必须经过一个统一的对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。

通用类图

备忘录模式

定义

Without violating encapsulation, capture and externalize an object' s internal state so that the object can be restored to this state later.
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象以外保存这个状态。这样之后就可将该对象恢复到原先保存的状态。

通用类图

访问者模式

定义

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
封装一些做用于某种数据结构中的各元素的操做,它能够在不改变数据结构的前提下定义做用于这些元素的新的操做。

通用类图

状态模式

定义

Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
当一个对象内在状态改变时容许其改变行为,这个对象看起来像改变了其类。

通用类图

解释器模式

定义

Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。

通用类图

享元模式

定义

Use sharing to support large numbers of fine- grained objects efficiently.
使用共享对象可有效地支持大量的细粒度的对象。

通用类图

桥梁模式

定义

Decouple an abstraction from its implementation so that the two can vary independently.
将抽象和实现解耦,使得二者能够独立地变化。

通用类图

相关文章
相关标签/搜索