Six theoretical knowledge of the principles of the six principles of the theory of knowledge of .NET .NET design patterns Design patterns

Transfer from 

.NET six principles of theoretical knowledge of design patterns

1. single responsibility principle (the SRP) (Single Responsibility Principle)
2. Alternatively Richter principle (the LSP) (the Liskov. Substitution Principle)
3. Dependency Inversion Principle (the DIP) (Dependence Inversion Principle)
4. Interface Segregation Principle (ISP) (Interface principle segregation)
5. The principle Dimitris (the LOD) (Law Of Demeter)
6. The opening and closing principle (OCP) (open closed principle)

 

 

 

1: Single Responsibility Principle (a class of only one thing)

Scenario:

Class T is responsible for two different functions: P1 responsibilities, P2 duties. When P1 demand political reform occurs sometimes need to modify the class T, it is likely to result in otherwise normal operation of the P2 fault.

Adhere to a single principle, with P2 to P1 is divided into two classes, each only concerned with one thing to avoid unnecessary trouble because the modification needs caused

 

Under what circumstances can a single breach of the principle responsibilities:

As long as the logic is simple enough, you can go against the principle of a single duty at the method level

If a class is simple enough, small enough methods, you can be a single violation of the principle of duty at the class level

Heteroaryl words: various levels (method / class / interface / library / project / system)

 

advantage:

Class Simple logic 1

2 code stable, high scalability

Disadvantages:

A plurality of code becomes

 

 

2: Principles Richter Alternatively (used anywhere in the base class, subclass can use a transparent (inheritance, polymorphism))

If a subclass can not have all the properties and behavior of the parent class, broken inheritance

Subclasses to modify the behavior of the parent class, use the override / virtual

In conclusion, Richter substitution principle is to standardize the use of inheritance

 

3: Dependency Inversion Principle (upper module not directly dependent on the lower module, should both (abstract classes / interfaces through abstract class dependent))

Abstract dependent (abstract class / interface classes), rather than on details (implementation class)

Details with respect to, dependent Abstract more stable

Abstract architecture based on more stable

Zatan: the words often heard

An inversion of control (IOC): the containers to complete dependence to a third party (destination)

2 dependency injection (DI): a means of control of inversion (behavior)

 

4: Isolation principle Interface (client should not rely on it does not require an interface, a class dependent on another class should be based on the smallest Interface)

Do not use large and interface methods to achieve such a result in undesired

Can not be split too small, this will cause inconvenience

About the interface segregation principle, the specific circumstances need to be based on business needs, not one and the same standards, the demolition of the close, had to measure their own needs

 

5: Law of Demeter (the least known principle) (an object in the closest relationship friends only communications, high cohesion, low coupling)

Before class to class, to avoid unnecessary dependencies

Schools, teachers, classes, four classes of students

School only communicate with the teacher, the teacher told the class communications only, not to the school while the teacher's association, and association classes, this will increase dependence,

 

Scene: The teacher has a class set of classes, class class has a collection of student

Error practice: the teacher in class to write a method to traverse all classes during the traversal, and traverse all students according to class (although such an approach so cool, but adds unnecessary dependencies)

The right approach: write a method, a method in class writing class, the class teacher to traverse all the classes in the class teacher, to get all the students in the class call the class traversal according to their class method (reducing unnecessary rely)

The relationship between class and class: Zatan

Vertical relationships: inheritance achieve

Horizontal relationship: dependent (internal method) associated with a combination of the polymerization (the latter three: method returns / parameter attributes)

 

 

6: Open Closed Principle (Open extended, close the Modify)

Without any guidance, it can only be regarded as a vision of the ideal state of architecture

Five other principles, the principle is to assist opening and closing, so as to achieve true ideal state

 

to sum up

1: The six principles does not refer to a specific technology or application, but rather a guiding principle, and how to follow six principles requires its own measure, the equivalent of a suggestion, does not necessarily have to follow six principles, according to business needs, their own make a choice

2: The real business needs, often appear to follow this principle, but another violation of the principles, which focus on specific principles, but also according to business needs to be, do not blindly pursue.

3: follow the six principles as much as possible to write code that allows your application to become a robust, highly scalable, easy to maintain.

Guess you like

Origin www.cnblogs.com/KQNLL/p/11876237.html