-
preface
-
1. Design principles
-
1.1. Open-close principle
-
1.2. Richter’s substitution principle
-
1.3. Dependency reversal principle
-
1.4. Interface isolation principle
-
1.5. Principles of synthesis/polymerization
-
1.6. Demeter’s Rule
preface
Before learning design patterns, it is important to understand the design principles, because the design of a system needs to follow certain principles, through which specific design patterns are implemented.
The design principles are as follows:
-
Open – close principle
-
Richter’s substitution principle
-
Rely on the inversion principle
-
Interface Isolation Principle
-
Synthesis/polymerization reuse principle
-
Demeter’s rule
1. Design principles
1.1. Open-close principle
The open closed principle states that a software entity should be closed for extension development and modification. Therefore, a module should be designed so that it can be extended without modification.
The design meeting the open and close principle can improve the superiority of the system: by extending the existing software system can provide new behavior to meet the new requirements of the software, so that the changing software system has a certain usability and flexibility; The existing software modules, especially the most important module of abstraction layer, cannot be modified, which makes the changing software system have certain stability and continuity.
How to implement the open close principle:
-
The first is abstraction. In Java, one or more abstract classes or interfaces can be given, specifying that all classes must provide characteristics as an abstraction layer for the class. That is, we usually implement the interface or abstract class, in the interface or abstract class to achieve the possible method, when the need for extension does not need to change the interface or abstract class, just need to implement the interface or inherit the abstract class, and rewrite the method, so as to achieve the extension closed to modify open.
-
The second aspect is encapsulation: encapsulation refers to the encapsulation of variability, which can be understood as finding the variables of a system and encapsulating them. Inheritance in Java is a form of encapsulation, in which we encapsulate some common elements into an object that we simply inherit when we need it. Inheritance should therefore be thought of as a way of encapsulating change, rather than as a way of generating special objects from common objects. At the same time, generics can also be understood as a kind of encapsulation. When you use generics, you do not fix the type, so that you can get the generic class through the encapsulation of generics, so that you can implement the desired type.
-
The above abstraction and encapsulation is a manifestation of the open-closed principle, which ensures that the open-closed principle is closed for extension and open for modification.
1.2. Richter’s substitution principle
The Richter substitution principle can be interpreted as saying that wherever a base class can appear, a subclass must appear. This statement can be understood in conjunction with the open close principle of inheritance. In Java inheritance guarantees the relationship between base class and subclass, so inheritance is the embodiment of abstraction. Therefore, the Principle of Richter’s substitution can be understood as the abstract realization of the open and closed principle.
So if you violate the Richter’s substitution rule you must violate the open close rule.
1.3. Dependency reversal principle
The dependency inversion principle is interpreted as relying on abstraction rather than implementation. The understanding of the sentence can be combined with abstract class or interface in Java, when a class implements an interface or inherited an abstract class, then the class is the implementation class, and we rely on is a layer of a class of abstract class or interface, if you have other class needs to implement method, cannot rely on the implementation class, but to rely on the abstract class.
For example, if there is an interface A, and class B implements interface A and overwrites its methods, then there is A class C, so class C needs to implement interface A, but cannot inherit from class B. If it inherits from class B, then it violates the dependency reversal principle. Because if you inherit from class B, then you violate the open close principle.
1.4. Interface isolation principle
This principle can be interpreted literally as interface isolation, and the principle is to provide clients with as small a single interface as possible, rather than a large total interface.
For example, when interfaces need to be exposed during normal development, you need to implement a certain interface isolation policy. Only the interfaces that need to be exposed are exposed, and some interfaces that do not need to be exposed are isolated. Therefore, the interface isolation principle is understood as a restriction on system communication. This principle restricts the system to communicate with the outside world as much as possible to avoid exposing too much communication. This ensures that the system does not transfer the pressure of modification to other objects during the function extension process.
1.5. Principles of synthesis/polymerization
The purpose of the synthesis/aggregation reuse principle is to use synthesis/aggregation rather than inheritance for reuse purposes. Is two kinds of relations in UML/polymerization synthesis is a kind of the most powerful relationship, and to control the subsidiary classes in the composite, which is in the synthesis of relationship need to distinguish between the whole and individual, and aggregation are primary relationship, in the paradigmatic relations are the individual classes, individual class aggregation can form a whole.
Therefore, the synthesis/polymerization reuse principle and the Richter substitution principle are complementary, both of which are the specification of the specific steps of the open and close principle. The synthesis/aggregation principle requires designers to consider the synthesis/aggregation relationship first, while the Richter substitution principle requires that when using inheritance relationship, it must be determined that the relationship is in accordance with certain conditions. Here it can be understood that inheritance is only necessary for related things, not random and blind inheritance.
1.6. Demeter’s Rule
Demeter’s law is understood to mean that a software entity should interact with as few other entities as possible. For this, it can be understood that some classes should not be associated with other classes as far as possible, and each entity class should be independent from each other as far as possible. In this way, if the class needs to be modified in the subsequent function expansion, other modules will not be affected.
If a class is associated with more than one class, changes to that class may affect other functional modules, causing instability in the entire system. At the same time to ensure that the system in the function expansion can be easily done to modify the closure.
References:
1. Java and Patterns