Design principles (2)

There are six principles of design patterns. These principles are the laws that have been constantly summarized by the code gods. The purpose is to improve the reusability of the code and reduce the coupling.

1 Principle of opening and closing

In 1988, Bertrand Meyer proposed the Open Close Principle in his book "Object Oriented Software Construction". The original text is as follows: " Software entities should be open for extension, but closed for modification ".

  • Meaning : The software module should be open for extension and closed for modification.
  • Example : When the program needs to add new functions, you cannot modify the original code, but add new codes to achieve a hot-swappable effect (hot-swappable: flexibly remove or add functions without affecting the original Function).
  • Purpose : In order to make the program extensible, easy to maintain and upgrade.

2 Principles of Richter Substitution

  • Meaning : The Liskov Substitution Principle is the cornerstone of inheritance and reuse. Only when the derived class can replace the base class and the function of the software unit is not affected, the base class can really be reused, and the derived class is also Ability to add new behaviors on the basis of the base class.
  • For example : Ball, originally a sporting goods, its derivative classes include basketball, football, volleyball, badminton, etc. If the derivative class replaces the original method of the base class, such as changing sporting goods to food supplies (then software The function of the unit is affected), which does not conform to the principle of Richter substitution.
  • Purpose : Specification of concrete steps to achieve abstraction.

3 Relying on the reversal

  • Meaning : Dependence Inversion Principle refers to interface programming, not implementation programming.
  • Example : Take a computer system as an example. Regardless of whether the motherboard, CPU, memory, or hardware is designed for the interface, if it is designed for implementation, the memory must correspond to the motherboard for a certain brand, then the motherboard will need to be replaced by the memory. The embarrassment also changed.
  • Purpose : to reduce the coupling between modules.

4 Interface isolation principle

  • Meaning : The Interface Segregation Principle is to use multiple isolated interfaces, which is better than using a single interface.
  • Example : For example, two interfaces belonging to the user module when logging in and registering are better than writing one interface.
  • Purpose : Improve program design flexibility.

5 Dimit's Law

Demeter Principle (Demeter Principle), also known as the Least Known Principle, was proposed by Ian Holland of Northeastern University in the fall of 1987 and popularized by Booch, one of the founders of UML. Later, it became widely known in the classic book "The Pragmatic Programmer".

  • Meaning : An entity should interact with other entities as little as possible to make the system functional modules relatively independent.
  • For example : the more public attributes or methods a class exposes, the greater the area involved in the modification, and the greater the risk spread caused by the change.
  • Purpose : Reduce the coupling between classes and reduce the dependence on other classes.

6 Single responsibility principle

Single responsibility principle (Single responsibility principle) by Robert C. Martin (Robert C. Martin) in the book "Agile Software Development: Principles, Patterns and Practices". Martin stated that this principle was developed based on the cohesion principle in the works of Tom DeMarco and Meilir Page-Jones.

  • 意思:一个类只负责一个功能领域中的相应职责,或者可以定义为:就一个类而言,应该只有一个引起它变化的原因。
  • 举例:该原则意思简单到不需要举例!
  • 目的:类的复杂性降低,可读性提高,可维护性提高。

刚入行的时候,在想什么样的代码是好代码?看到很多前辈的文字都说好的代码要符合「高内聚,低耦合」, 其实就是:

高内聚低耦合?

  • 内聚是从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事。它描述的是模块内的功能联系;
  • 耦合是软件结构中各模块之间相互连接的一种度量,耦合强弱取决于模块间接口的复杂程度、进入或访问一个模块的点以及通过接口的数据。

参考资料:

  1. 什么是设计模式?
更多信息请参考千锋php,千锋论坛

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/72676294