before we start the Android software app, we need to make a preliminary understanding of object-oriented design principles and design patterns. Talent in the future combat process, less detours. Make our software development career feel happy and relaxed. Okay, nonsense, let's talk about the software development design principles in soft oop today. These things are the essence of the design of OOP, they contain the product of the predecessor left. Now. The basic principles of software design are as follows (there are 11 common types): Single duty principle, open closure principle, dependency inversion principle, interface isolation principle and Richter replacement (Liskov substitution) principle
Single principle of responsibility
is a class value to do a thing. The only one that has caused it to change.
We often hear a word "high cohesion, low coupling". We define responsibility as the cause of the change. To improve cohesion to reduce the cause of the change.
The greater the responsibility, the higher the coupling, and the more we change, the more it will spread to other classes.
Open Closure Principle
Local Detail now: to expand development, to change the closure.
What do you mean? Openness to extensions means that existing code can be extended when there are new requirements or changes. To adapt to the new situation; means that once the class has been designed. To be able to complete their work independently, without any attempt to change it.
In fact, we use the most in the actual programming process.
You think: generally when we design, we will design abstract classes, interfaces (behavior), and then the implementation of the details of the class is through inheritance, we write different detailed implementation of the method of implementing the class, which is the true portrayal of this design principle.
Dependency Inversion principle
The core idea is that it relies on abstraction.
In detail, the high-level modules do not depend on the underlying modules, both of which are dependent on abstraction, and the abstraction is not dependent on detail, but on abstraction in detail. When there is a tight coupling between the two modules, the best way is to separate the interfaces and implementations: Define an abstract interface between dependencies so that the high-level module invokes the interface. And the bottom module realizes the definition of the interface, so as to effectively control the coupling relation and achieve the design target which relies on abstraction.
Abstract stability determines the stability of the system, because the abstraction is invariant, the dependence on abstraction is the essence of object-oriented design, but also the core of dependency inversion principle. Dependency on abstraction is a common principle. Sometimes it is unavoidable to rely on details, and the trade-offs between abstraction and detail must be weighed. The method is not a one-layer constant. Depending on the abstraction, it is programming the interface. Do not program the implementation.
Interface Isolation principle
The core idea is to use multiple small, dedicated interfaces instead of a large total interface. when we were designing the interface. In detail, the interface isolation principle body now: The interface should be cohesive, should avoid the "fat" interface. The dependency of a class on another class should be based on the smallest interface, and do not force reliance on the unused method, which is an interface pollution.
Richter Replacement (Liskov substitution) principle
no matter where the base class can appear, subclasses must be able to appear. LSP is the cornerstone of inheritance reuse. Only when a subclass can replace a base class, the functionality of a software unit is not affected, the base class talent is actually reused, and subclasses can add new behavior based on the base class. The Liskov substitution principle mainly focuses on the establishment of abstraction and polymorphism on the basis of inheritance, and therefore only follows the principle of Liskov substitution. The ability to ensure that inheritance is reused is reliable. The method of implementation is interface-oriented programming: The public part is abstracted as a base class interface or abstract class, by extract abstract class. In subclasses, the same responsibilities are supported in new ways by covering the methods of the parent class.
Principles of software design for object-oriented programming