Study summary of the six principles of design patterns 1

Design pattern: In the process of object-oriented language development, when encountering various scenarios and problems, the ideas for solving problems are proposed. Design patterns are a routine to solve specific problems.

Six principles of design patterns: In the process of object-oriented language development, some guiding principles are recommended without clear tricks, and they are often ignored or violated.

 

table of Contents

1. Single responsibility principle:

2. The principle of Richter substitution:

3. Dimit's law:


1. Single responsibility principle:

Definition: Class T is responsible for two different responsibilities: responsibility P1 and responsibility P2. When the class T needs to be modified due to changes in the requirements of P1, it may cause failure of the originally functioning P2.

Single responsibility principle at the method level: one method is responsible for only one thing (different responsibilities are split into multiple methods, and branch logic is split);

Single responsibility principle at the class level: a class is only responsible for one thing;

Single responsibility principle at the class library level: a class library should have clear responsibilities;

The principle of single responsibility at the project level: a project should have clear responsibilities (client, management background, background service, timing tasks, distributed engine);

Single responsibility principle at the system level: split into systems for general functions (IP positioning, logs, online statistics);

 

Advantages of the single responsibility principle:

1. Reduce the complexity of the code, a class (method, class library, project, system) is responsible for one thing, and the logic is simple.

2. The code is highly readable and easy to maintain.

 

Disadvantages of the single responsibility principle:

1. If the code is split too much, the amount of code will increase.

2. There are more types and more methods, and the cost of using (understanding) increases.

 

Use of the single responsibility principle:

If the type is simple enough and there are few methods, the single responsibility can be violated at the class level; if the type is complex and there are many methods, it is recommended to follow the single responsibility principle;

 

2. The principle of Richter substitution:

Definition: Wherever a base class is used, its subclasses can be used transparently.

The essence of the Richter substitution principle is to guide the better use of inheritance.

1. Some method attributes in the parent class are not in the child class, so the child class should not inherit the parent class anymore.

2. Subclasses can implement the abstract methods of the parent class, but the parent class cannot override the non-abstract methods of the parent class.

All the methods that have been implemented in the parent class are actually designing a series of specifications and contracts. Although it does not force all subclasses to comply with these specifications, if the subclass arbitrarily modifies these non-abstract methods, it will Cause damage to the entire inheritance system.

 

Look at the following example: there is an abstract class of animals, the abstract class defines animals that can eat and sleep. There is a dog class that inherits the animal class and implements the abstract methods of its parent class.

Results of the:

 

 

The above example is a normal parent-child inheritance situation. If you want to override the non-abstract method of the parent class in the child class, the Dog class must override the Sleep method of BaseAnimal, as shown below:

 

 In this case, the execution result changes:

This situation violates the Richter substitution principle. Either the Dog class no longer inherits BaseAnimal, or the Dog class does not override the Sleep method of the parent class. 

The subclass overrides the non-abstract methods of the parent class, and the syntax is of course no problem. It's just not recommended from the design.

 

3. Dimit's law:

Dimit's law is also called the least-know principle. A software entity should interact with other entities as little as possible.

A very vivid sentence is to communicate only with the most direct friends.

 

Example: The relationship between a celebrity and an agent.

Because celebrities devote themselves to art, many daily affairs are handled by their agents, such as meetings with fans and business negotiations with media companies. The agents here are friends of celebrities, and fans and media companies are strangers, so Dimit's rule is suitable.

Manager human:

Fan category:

Company category:

Star category:

 

Procedure call:

Results of the:

 

The application of Dimit's law in specific procedures:

1. Minimize internal dependence.

2. Reduce access modifiers and only expose methods or properties that should be exposed.

Use of private, protected, internal, and public

 

Participate in the detailed explanation of design patterns: http://c.biancheng.net/design_pattern/

 

Guess you like

Origin blog.csdn.net/liangmengbk/article/details/113038150