The seven fundamental principles of object-oriented:
Principle a single duty principle
Single Duty principle (Srp:single responsibility principle) also known as single function principle
Core: Decoupling and enhanced cohesion (cohesion poly, low coupling).
Principle two: The principle of the Richter replacement (Lsp:liskov Substitution Principle)
Core: Wherever the parent class appears, it can be substituted with his subclass (the subclass should be able to replace the parent class and appear anywhere the parent class can appear)
Four levels of meaning:
(1) Subclasses must fully implement the methods of the parent class. Calling other classes in a class is a must to use the parent class or interface, and if the parent class or interface cannot be used, then the design of the class has violated the LSP principle.
(2) Subclasses can have their own personalities. Subclasses can of course have their own behavior and appearance, that is, methods and properties
(3) When overriding or implementing a method of the parent class, the input parameters can be magnified. That is, the subclass can overload the parent class's methods, but the input parameters should be larger than the parent class method, so that when the subclass is substituted for the parent class, the call is still a method of the parent class. That is, the pre-condition of a method in a subclass must be the same or looser than the predecessor of the method overridden in the superclass.
(4) The output can be scaled down when overriding or implementing a method of the parent class.
Principle three: The principle of Dependency injection
Dependency Injection principle (dip:dependence inversion Principle)
Alias: Dependency inversion principle or dependency reversal principle
Core: To rely on abstraction, do not rely on specific implementations
Three layer meaning:
(1) High-level modules should not rely on low-layer modules, both should rely on their abstraction (abstract class or interface);
(2) Abstraction should not rely on details (concrete implementation);
(3) Detail (concrete implementation) should be dependent on abstraction.
Three ways to achieve:
1, through the construction function to pass the dependent object;
2, passing the dependent object through setter method;
3. Interface declaration implements the performance of dependent objects in Java:
(1) The dependencies between modules are generated by abstraction, and no direct dependency between classes is realized, and their dependencies are produced by interfaces or abstract classes;
(2) The interface or abstract class does not depend on the implementation class;
(3) Implement a class-dependent interface or abstract class.
Principle four: interface separation principle
Interface separation principle (Isp:interface segregation Principle) alias interface Isolation principle
Core idea:
Client programs should not be forced to rely on methods that they do not need to use.
The meaning of the principle of interface separation
Think is: An interface does not need to provide too much behavior, an interface should only provide an external function, should not be all operations are encapsulated in an interface
Definition of the interface isolation principle
The first definition: a client should not rely on an interface that it does not need.
The second definition: the dependency of a class on another class should be based on the smallest interface.
The interface is divided into the following two kinds:
Object Interface A class declared in Java, an instance produced by the new keyword, a description of a type of thing, is also an interface.
Class Interface interface defined by the keyword Interface.
Two implementation methods for separating interfaces:
(1) Separating the interface with a delegate. (Separation through delegation) is the adapter mode (Adapter), which delegates the request to the implementation class of the other interface to complete the required duties.
(2) Detach the interface using multiple inheritance. (Separation through multiple inheritance. This method accomplishes the required responsibilities by implementing multiple interfaces.
There are pros and cons in both ways, usually we should consider the latter scenario first, and select the previous one if the type conversion is involved.
Principle five: Opening and closing principle
Opening and closing principle (Ocp:open Closed Principle)
Core idea: Open to extensions, close for modifications. That is, when designing a module, it should be allowed to be extended without modification.
According to the opening and shutting principle, when designing a software system module (class, method), it should be able to expand its function (extended open) on the basis of not modifying the original module (modify close).
Extended Open: The function of a module is extensible, and the module is extended and open. The functional scalability of software systems requires that modules be extended and open.
Modify Close: A module is called by another module, if the module's source code is not allowed to modify, then the module modified closed. The functional stability of the software system, the continuity requirement is modified to be closed.
The realization method of opening and closing principle in order to satisfy the principle of closed-closing (closed for modification) and the extended open for extension principle, the invariant parts of the software system should be abstracted, in the face of
In the design of the object:
(1), these invariant parts can be abstracted into an immutable interface, these immutable interfaces can deal with future expansion;
(2), the minimum function design principle of the interface. According to this principle, the original interface can deal with the future expansion;
(3), the insufficient part can be achieved by defining a new interface;
(4), the call between the modules through an abstract interface, so that even if the implementation layer changes, there is no need to modify the caller's code.
The interface can be reused, but the implementation of the interface is not necessarily reusable. The interface is stable, closed, but the implementation of the interface is variable and open.
Through the different implementation of the interface and the inheritance of the class of the system to add new or change the original function of the system, to achieve soft expansion of the software system.
To put it simply,
Whether the software system has good interface (abstract) design is an important criterion to judge whether the software system satisfies the opening and shutting principle. Now many of the open and close principle is equivalent to interface-oriented software design.
The construction of the Relativity software system of opening and closing principle is a process that needs to be reconstructed constantly, in this process, the function abstraction of module, the relation between module and module, will not be very clear from the beginning, so build 100% meet the
The closed principle of the software system is quite difficult, which is the relativity of the open and close principle. But in the design process, through the abstraction of the function of the module (interface definition), the abstraction of the relationship between modules (through the interface call), abstraction and implementation separation (interface-oriented programming), etc., can be as close as possible to meet the opening and closing principle.
Principle VI: Dimitri Law
Dimitri Law (Lod:law of Demeter) also known as the least knowledge principle (Least knowledge Principle shorthand Lkp)
Core idea: An object should have as little knowledge of other objects as possible and not talk to strangers. (Decoupling between classes, low coupling) means to reduce the coupling between the various objects, improve the maintainability of the system, in the module only through the interface to communicate, regardless of the module's internal working principle, can make the coupling of the various modules Chengdu to the minimum, promote the reuse of software.
There are several points to note when applying the Dimitri rule to the design of the system:
① in the division of classes, a class with weak coupling should be created;
② in the structure design of a class, each class should minimize the access rights of members
Object-oriented seven basic design principles, we'll talk about it today.