This is the first day of my participation in the August Challenge. For details, see:August is more challenging

The Single Responsibility Principle

define

There should never be more than one reason for a class to change.

advantages

  • Class complexity is reduced, and what responsibilities are clearly defined;
  • More readability, less complexity, of course more readability;
  • It’s more maintainable, it’s more readable, it’s easier to maintain;
  • Lower risk caused by the change, change is necessary, do better if the single responsibility of the interface, an interface to modify only have an effect on the corresponding implementation class, no effect to the rest of the interface, the system scalability and maintainability are very big help, single responsibility is suitable for the interface, class, also applies to the method

disadvantages

  • The definition of responsibilities is vague and there is no quantitative standard

Best practices

For the single responsibility principle, it is recommended that the interface must be single responsibility, and the design of the class should be as simple as possible for only one reason to cause changes.

Liskov Substitution Principle

define

  • The first and most authentic definition: If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T,the Behavior of P is unchanged when O1 is substituted for O2 then S is a subtype of T. (If for every object O1 of type S, there is an object O2 of type T, If the behavior of program P defined in terms of T does not change when o1 is replaced by O2, then S is a subtype of T.
  • Second definition: Functions that use pointers or references to base classes must be able to use objects of derived classes without knowing All references to the base class must transparently use the objects of its subclasses

That is, as long as the superclass can appear where the subclass can appear, and the replacement of the subclass will not cause any error or exception, the user may not need to know whether the superclass or the subclass. However, the opposite is not true, where there are subclasses, the parent class may not be able to adapt

The Richter substitution principle defines a specification for good inheritance, including the following four points

  1. A child class must fully implement the methods of its parent class
  2. Subclasses can have their own personalities
  3. Input parameters can be amplified when overriding or implementing a method of the parent class (i.e. the type of the input parameter is wider than the type coverage of the parent class)
  4. The output can be narrowed when overridden or implemented by methods of the parent class

Best practices

In the project, when using the Richter substitution principle, try to avoid the “personality” of the subclass, once the subclass has “personality”, the relationship between the subclass and the parent class is difficult to reconcile, use the subclass as the parent class, the “personality” of the subclass is eliminated — a bit of grievance; Using subclasses as a separate business confuses the coupling between the code — there is no standard for class substitution

Dependence Inversion Principle

The realization of every logic is composed of atomic logic. The indivisible atomic logic is the low-level module, and the reassembly of atomic logic is the high-level module. Abstract refers to an interface or abstract class, neither of which can be instantiated directly; A detail is an implementation class. A class that implements an interface or inherits an abstract class is a detail. The characteristic is that it can be instantiated directly

define

  • High-level modules should not depend on low-level modules, both should depend on their abstractions;
  • Abstractions should not depend on details;
  • Details should depend on abstractions.

The dependency inversion principle is expressed in THE PHP language

  • The dependencies between modules occur through abstraction, and the dependencies between implementation classes do not occur directly. The dependencies are generated through interfaces or abstract classes.
  • An interface or abstract class does not depend on an implementation class;
  • Implementation classes depend on interfaces or abstract classes

advantages

The principle of dependency inversion can reduce the coupling between classes, improve the stability of the system, reduce the risk caused by parallel development, and improve the readability and maintainability of the code

Three ways to implement the dependency

  1. The constructor passes the dependent object and declares the dependent object in the class through the constructor. In dependency injection terms, this is called constructor injection
  2. The Setter method passes the dependency object, sets the Setter method in the abstraction and declares the dependency, in terms of dependency injection, this is a Setter dependency injection, and it’s injected that way
  3. An interface declares a dependency object in a method of the interface, also known as interface injection.

Best practices

  1. Every class tries to have an interface or an abstract class, or both
  2. The surface type of the variable should be either an interface or an abstract class
  3. No class should be derived from a concrete class
  4. Try not to overwrite a method of the base class (if the base class is an abstract class and the method is already implemented, the subclass should try not to overwrite it. Dependencies between classes are abstract. Overwriting abstract methods has an impact on the stability of dependencies.
  5. Use in conjunction with the Richter substitution principle

Interface Isolation Principle (ISP)

define

A client should not rely on interfaces it does not need, and dependencies between classes should be built on the smallest interface

The client depends on the interface it needs, and the client provides the interface it needs. If you eliminate the interfaces that are not needed, then you need to refine the interface to ensure its purity

Interface segregation principle and single responsibility look Angle is not the same, the single single classes and interfaces are required duties, notice is a duty, this is the division of business logic, and the interface segregation principle requires the method of the interface, less as far as possible according to the resolution of the interface segregation principle interface, you must first meet the single responsibility principle

The interface isolation principle is used to restrict interfaces. It has the following four meanings:

  1. The interface should be as small as possible
  2. The interface should be highly cohesive
  3. Custom service
  4. There are limits to interface design

Best practices

An interface only serves one module or business logic;

Compress public methods in the interface through business logic;

If the interface has been polluted, try to modify it. If the risk is high, adapter mode transformation is adopted.

Understand the environment, refuse to follow blindly.

Law of Demeter

Also known as Least Knowledge Principle (LKP)

An object should have minimal knowledge of other objects

Only communicate with friends. Classes that appear in the input and output parameters of member variables and methods are called friends. Relationships between classes are built between classes, not between methods, so a method should try not to introduce objects that do not exist in the class.

There is also distance between friends. The more public properties or methods a class exposes, the greater the surface involved in modification, and the greater the spread of risk caused by the change. Therefore, you need to repeatedly evaluate whether you can reduce public methods or properties at design time.

If a method is placed in this class and does not add to the relationship between classes or negatively affect the class, then it is placed in this class.

Use Serializable with caution

The Open Closed Principle

define

Definition: a software entity such as a class, module, or function should be open for extension, but closed for modification. The first five principles are the tools and methods that guide the design, and the Open closed principle is its spiritual leader.

Importance:

Extend operations to avoid modifying unit tests and regression tests; Improve code reusability and reduce the granularity of logic until a logic can no longer be split; Improve maintainability; Object-oriented development requirements.

use

Abstract the constraint

The extension is restricted by the interface or abstract class, and the extension is not allowed to appear in public methods that do not exist in the interface or abstract class.

Use interfaces or abstract classes rather than implementation classes for parameter types and reference objects.

The abstraction layer is kept as stable as possible and is not allowed to change once it is determined.

Metadata controls module behavior by extending a subclass and modifying configuration files to complete business changes such as dependency injection.

Formulate the project charter;

Encapsulate the changes, find the points of expected change or instability, and create stable interfaces for those points of change

Encapsulate the same changes into an interface or abstract class;

There should not be two different variations in the same interface or abstract class