-
类单一职责原则(Single responsibility principle):
There should never be more than one reason for a class to change.
一个类只有一个引起这个类变化的原因。即一个类只完成一个功能,如果做不到一个类只完成一个功能,最少要保证一个方法只完成一个功能。
此原则的核心就是解耦和增强内聚性。 -
依赖倒置原则(Dependence Inversion Principle):
High level modules should not depends upon low level modules.Both should depend upon abstractions.Abstractions should not depend upon details.Details should depend upon abstractions.
程序要依赖于抽象接口,不要依赖于具体实现
高层组件应该依赖抽象而不依赖具体,即面向接口编程,一般依赖的成员变量或者参数都应该是抽象的不应该是具体的。 -
里氏代换原则(Liskov Substitution Principle):
Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing it.
凡是父类出现的地方都可以用子类代替并且原功能没有发生变化,子类不应该覆盖父类的非抽象方法。在继承类是,务必重写(override)父类中所有的方法,尤其需要注意父类的protected方法(它们往往是让你重写的),子类尽量不要暴露自己的public方法供外界调用 -
迪米特法则(Law of Demeter)又叫作最少知识原则(Least Knowledge Principle 简写LKP):
talk only to your immediate friends。降低耦合性
一个类要尽量的封装自己,一个类只与自己的朋友类打交道一般朋友类是成员变量或者参数,非朋友类一般都是局部变量 -
接口隔离原则( Interface Segregation Principle):
The dependency of one class to another one should depend on the smallest possible interface.
客户端不应该依赖它不需要的接口;
一个类对另一个类的依赖应该建立在最小的接口上。
一个接口完成的功能尽可能的单一,不要让一个接口承担过多的责任。 -
开闭原则(Open Closed Principle ):
Software entities like classes,modules and functions should be open for extension but closed for modifications.
对扩展开放,对修改闭合