Declarative: After Reading <Java and mode>, this article does not mean correctness.
I believe that in the course of learning, I will constantly update this article, and ultimately achieve a thorough understanding.
Some time ago, I saw <Java and mode>, because I didn't care too much about the design principles, I could only know it, but I didn't know why. even when discussing with others, I do not quite understand the advantages of the model or can clearly explain a model. Therefore, it is necessary to record my experiences and understanding here.
First, we need to understand the three principles of object-oriented (encapsulation, inheritance, polymorphism ).
Design Pattern prerequisites:
(1) programming for interfaces, rather than implementing Programming
(2) prioritize the use of object combinations, rather than inheritance.
(3) Encapsulation changes
Implementation of specific principles:
Open-Close principle:
The core principle of the design pattern is"Open-Close principle ",All of them are based on this principle.
The principle is as follows:Disable modification and open Extension.
That is to say, a good system should not be modifiedCodeTo expand the new functions.
To achieve this, a key is high abstraction. When designing a system, we need to consider some variable factors and abstract these variable parts.
That is to say, the abstract class or interface should not be modified, and the specific implemented class should be allowed to be extended.
For example:
Factory method
The factory method meets the "open-close principle ".
Lee's replacement principle:
This principle is about : Where any parent class can appear, its subclass can also appear.
<Java and mode> the book describes: If there is a class C object C1 for each type of P object P1ProgramIn a, after P1 is replaced with C1, the behavior function of program a remains the same, then C is the subtype of P (I personally feel that this explanation is too long.)
Personally, this is the relationship between is-.
However, this mode, in turn, is not necessarily correct. And where the child class can appear, not necessarily the parent class.
For example: Code
Public Abstract Class A
{
Public A ()
{
}
Public Void Virtual Amethod ()
{
Printf ("This is");
}
}
Public Class B:
{
Public B ()
{
}
Public Void Overrid amethod ()
{
Printf ("This is");
}
Public Void Bmethod ()
{
}
}
Void Test ()
{
A. amethod ();
}
Void Test2 (B)
{
B. bmethod ();
}
Void Main ()
{
A= NewB ();
Test ();
B= NewB ();
Test (B );
Test2 (B );
Test2 ();
}
The column above has not been tested, but it should be like this.
And test (A) meet this principle, Test2 (B) is the inverse column.
Dependency reversal principle:
The principle is as follows:It depends on abstraction instead of specific implementation.
If the "open-close principle" is the design goal, the dependency reversal principle is the means to achieve this goal.
It applies to the code and the code above, test (A). The function and parameters should be abstract or interface, rather than concrete implementation Class B.
Synthesis/aggregation principles:
The principle is as follows:Synthesis or aggregation should be used as much as possible, rather than inheritance;
And achieve the relationship of has-a, that is, try to use the original object in a new object, so that this object is part of the new object. the new object achieves the purpose of reusing the original object function through some delegation of the original object.
Example reference:
Adapter ModeObject adaptation
Dimit rules:
The principle is as follows:Minimize interaction and reduce coupling between classes.
<Java and mode> the book says: only communicate with your most direct friend
Column reference:
Facade Mode
Interface isolation principle:
The principle is as follows:The interface should be as small as possible to reduce interface pollution
Other onlineArticleConnection:
Detailed description of Design Pattern principles
Design principles (Arrangement)
The cornerstone of object-oriented design-the principle of opening and closing