文章目录
单一职责原则(Single Responsibility Principle)
There should never be more than one reason for a class to change.
总结,一个接口或者一个类,尽量做到,只做一件事情,清晰,明显。
里氏替换原则(Liskov Substitution Principle)
Software entities like classes,modules and functions should be open for extension but closed for modifications.
子类替换基类没有任何影响,类似继承的概念
依赖倒置原则(Dependence Inversion Principle,DIP)
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.
模块之间的依赖是通过抽象类或者接口,总结,就是面向接口编程
接口隔离原则
- 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.
通过不用业务模块,将接口分割出来,总结,接口尽量细化。
迪米特法则(Law of Demeter)
Only talk to your immediate friends
类之间解耦合,只要暴露和其他类习惯关联的方法,就是用public修饰,其他的用private,尽量高内聚。
开闭原则(Open Close Principle)
Software entities like classes,modules and functions should be open for extension but closed for modifications.
开放扩展,关闭修改。