因为我们的代码并不是一成不变的,总会随着需求而不断发生变化,如果我们没有遵循一定的设计模式,那么代码迁移的成本可能就会非常高,所以我们需要设计模式来指导我们编写代码,进而增加代码的扩展性。设计原则则是23种设计模式的抽象概述,设计模式是设计原则的具体实现。
1. 设计原则
分类
设计原则一共有六种,分别为
单一职责原则(S)
单一职责原则,即SRP(Single Responsibility Principle)。
定义为:应该有且仅有一个原因引起类的变更(There should never be more than one reason for a class to change.)。
开闭原则(O)
开闭原则,即OCP(Open Closed Principle)。
定义为:一个软件实体,如类、模块和函数应该对扩展开发,对修改关闭(Softwate entities like classes,modules and functions should be open for extension but closed for modifications.)。
里氏替换原则(L)
里氏替换原则,即LSP(Liskov Substitution Principle)。
定义为:
- 如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有发生变化,那么类型S是类型T的子类型(If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T,the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.)。
- 所有引用基类的地方必须能透明地使用其子类的对象(Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.)。
迪米特法则(L)
迪米特法则,即LoD(Law of Demeter)
又叫最少知识原则,即LKP(Least Knowledge Principle)。
定义为:一个对象应该对其他对象有最少的了解
- 只和朋友交流,不和陌生类交流
- 朋友之间也是有距离的,尽量减少public方法
- 是自己的就是自己的,如果一个方法放在本类中,既不增加类间关系,也对本类不产生负面影响,那就放在本类中
- 谨慎使用Serializable
接口隔离原则(I)
接口隔离原则,即ISP(Interface Segregation Principle)
定义为:
- 高层模块不应该依赖低层模块,两者都应该依赖其抽象;
- 抽象不应该依赖其细节;
- 细节应该依赖抽象。
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.
依赖倒置原则(D)
依赖倒置原则,即DIP(Dependence Inversion Principle)
定义为
- 客户端不应该依赖它不需要的接口(Clients should not be forced to depend upon interfaces that they don’t use.)。
- 类间的依赖关系应该建立在最小的接口上(The dependency of one class to another one should depend on the smallest possible interface.)。
总结
以上六大设计原则,取其缩写的首字母(重复的去掉),联合起来就是SOLID,含义就是建立稳定、灵活,健壮的设计,其中开闭原则是以上六种设计原则中最重要的一个原则,也是最基础的原则,其他几种原则或多或少都有开闭原则的影子。
2. 设计模式
设计模式共有23中,总共可以分为三大类,此外还有其他两种模式
分类
创建型模式
共5种:
-
单例模式
确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。(Ensure a class only one instance, and provide a global point of access to it.)
-
工厂方法模式
定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到子类。(Define an interface for creating an object, but let subclasses decide which class 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.)
-
建造者模式
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。(Separate the construction of a complex object from its representation so that the same construction process can create different representations.)
-
原型模式
用原型实例指定创建对象的种类,并且通过拷贝这些原型对象创建新的对象。(Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.)
结构型模式
共7种:
-
适配器模式
将一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。(Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.)
-
装饰器模式
动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更加灵活。(Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.)
-
代理模式
为其他对象提供一种代理以控制对这个对象的访问。(Provide a surrogate or placeholder for another object to control access to it.)
-
外观模式
要求一个子系统的外部与其内部的通信必须通过一个统一的对象进行。门面模式提供一个高层次的接口,使得子系统更易于使用。(Provide a unified interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.)
-
桥接模式
将抽象和实现解耦,使得两者可以独立地变化。(Decouple an abstraction from its implementation so that the two can vary independently.)
-
组合模式
将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。(Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.)
-
享元模式
使用共享对象可有效地支持大量的细粒度的对象。(Use sharing to support large numbers of fine-grained objects efficiently.)
行为型模式
-
策略模式
定义一组算法,将每个算法都封装起来,并且使它们之间可以互换。(Define a family of algorithms, encapsulate each one, and make them interchangeable.)
-
模板方法模式
定义一个操作中的算法的框架,而将一些步骤延迟到子类中。使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。(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.)
-
观察者模式
定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。(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 way to access the elements of an aggregate object sequentially without exposing its underlying representation.)
-
责任链模式
使多个对象都有机会处理请求,从而避免了请求的发送者和接受者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止。(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.)
-
命令模式
将一个请求封装成一个对象,从而让你使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。(Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.)
-
备忘录模式
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可以将该对象恢复到原先保存的状态。(Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.)
-
状态模式
当一个对象内在状态改变时允许其改变行为,这个对象看起来像改变了其类。(Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.)
-
访问者模式
封装一些作用于某种数据结构中的各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作。(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.)
-
中介者模式
用一个中介对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使其耦合松散,而且可以独立地改变它们之间的交互。(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.)
-
解释器模式
给定一门语言,定义它的文法的一种表示,并定义一个解释器,该解释器使用该表示来解释语言中的句子。(Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.)
补充:
-
并发型模式
处理多线程范式
-
线程池模式
