Six principles of design patterns

I. Six principles of design pattern


The name of the explain
0. Single Responsibility Principle (SRP) There should be only one reason for a class to change.
I. “Open-closed” Principle (OCP) In software design patterns, the idea that you can’t modify, but you can expand, is one of the most important design principles. That is, software entities (classes, templates, functions, and so on) should be extensible but not modifiable. 【 popular 】 : When designing, always consider, try to make this class is good enough, write good do not go to modify, if new requirements come, we add some classes are done, the original code can not move.
Ii. Richter’s Substitution Principle (LSP) 1. A software entity that uses a superclass must apply to that subclass, and it cannot tell the difference between a superclass object and a subclass object. That is, in software, the behavior of the program does not change if the parent class is replaced by its subclasses. 【 a word 】 :Child types must be able to replace their parent types.
Iii. Dependency Inversion Principle (DIP) 1. High-level modules should not depend on low-level modules. Both should rely on abstraction. 2. Abstraction should not depend on details, details depend on abstractions (plain English) : program for interfaces, not implementations.
4. Interface Isolation Principle (ISP) 1. It is better to have multiple specialized interfaces than a single master interface. In other words, from a client class’s point of view: the dependency of one class on another should be based on a minimal interface. 2. A bloated interface pollutes the interface. Customers should not be forced to rely on methods they do not use.
5. Principles of Synthesis/Polymerization reuse (CARP) Use composition/aggregation whenever possible and avoid class inheritance whenever possible. [Aggregation] : indicates A weak ownership relationship, indicating that an object A can contain an object B, but the object B is not part of an object A. [Synthesis] : A strong ownership relationship, drawing on the strict relationship between the parts and the whole, the life cycle of the parts and the whole is consistent.
Demeter’s Rule (LoD) least knowledge principle Emphasize loose coupling between classes. That is, if two classes do not have to communicate directly with each other, then two classes should not send direct interactions. If one class needs to call a method of another class, the call can be forwarded by a third party.

___ Most of the content is taken from The Big Talk Design Pattern

1. The original poster? Isn’t the title 6 design patterns? Why are there seven?

A: The list varies from book to book. The single schema principle and the interface isolation principle are mostly mentioned in one. This paper is listed above, and detailed analysis will be given after further exploration.

2. Isn’t the interface isolation principle the same as the single responsibility principle?

A: Wrong, the interface isolation principle is not viewed from the same perspective as a single responsibility.

Single responsibility requires a single class and interface responsibility, focusing on responsibility, which is the division of business logic;

The interface isolation principle requires that interfaces have as few methods as possible. For example, an interface may contain 10 method, the duty of the 10 methods in an interface, and provides access to multiple modules, each module according to the limits of their authority to access, outside the system by document constraints “do not use the method of don’t access”, according to the single responsibility principle is allowed, according to the interface segregation principle is not allowed, Because it requires “as many specialized interfaces as possible,” what do specialized interfaces mean? This means that each module should be provided with a single interface, and several modules should have several interfaces, rather than creating a large, bloated interface that accommodates all client access.