设计模式的基本原则
- 单一职能原则 Single Responsibility Principle
A class should have one and only one reason to change, meaning that a class should have only one job.
每一个类只拥有一项功能。
-
开闭原则 Open Close Principle
- 对扩展开放,对修改关闭
-
里氏替换原则 Liskov Substitution Principle
- 任何基类出现的地方,子类一定可以出现
接口隔离原则 Interface Segregation Principle
A client should never be forced to implement an interface that it doesn’t use or clients shouldn’t be forced to depend on methods they do not use.
接口要小而专,不能大而全
使用多个隔离的接口,比使用单个接口要好
依赖倒置原则 Dependency Inversion Principle
Entities must depend on abstractions not on concretions. It states that the high level module must not depend on the low level module, but they should depend on abstractions.
- 针对接口编程
- 抽象不能依赖于具体,具体应当依赖于抽象
引用:
S.O.L.I.D: The First 5 Principles of Object Oriented Design