I will keep maintaining this article and it will grow longer and longer, but it is about some of my thoughts on Object-Oriented learning. I hope it will be useful to everyone.
Explain the reasons for writing this article before you start. The reason is the design pattern. Because the various patterns in the design patterns are built on these principles. For example, building a house requires a solid foundation, or comparing it to the principle used in mathematical arguments. You can't say why building a house must be built on the foundation, or why 2.1 straight lines and 3.1 faces are so awesome, because this is a natural law, you must follow them. These design principles are similar. They are not as elegant as the 24 design patterns, but they are the true rules of programming. Many Masters have summed up these principles and told us these elders. When we understand them, we can better design excellent software frameworks.
First, let's take a look at the knowledge popularization. If you see Ood in some articles, you need to understand that this is object-oriented design (OOD ). If some design books mention Ood, we will know that this refers to object-oriented design.
Next, the real protagonists will be on the stage. We hope we can fully understand these principles.
Seven major design principles of Ood 1: open and closed principles (open-closed principle, OCP)This principle was proposed by Master Bertrand Meyer in 1988, when I was 1 year old. This principle is defined as follows:
Software entities shoshould be open for extension, but closed for modification. How simple ?! This principle means:
The software should be open to expansion, and the modification should be closed.More broadly speaking, that is to say, we have developed a software that should be able to expand (open) its functions, you do not need to modify (close) the original program ).
Ladies and gentlemen, if we have been developing for a period of time, we will find that it would be great for a software code design to be able to expand functions and disable modifications. This means that the stability of the software maintenance will be greatly improved, because the modification is disabled, so the previous Code does not need to be modified too much, we have done full-text search, copy and replace some files, or repeatedly modify some code in many files. If our software code can be written according to the principle of extension development and modification and closure, we will try our best to make this principle better reflected in the software design, this is a great design.
To sum up, in accordance with the OCP principles, software design and development will have the following advantages:
1. Flexible software availability. You can extend the software and add new features in the software. In this way, this software can meet the ever-changing new needs by constantly adding new modules!
2. Because the original module of the software cannot be modified, you do not have to worry about the stability of the software.
The implementation of OCP mainly uses abstraction. Ladies and gentlemen, do you often use abstract ideas when writing code. You must use abstract ideas to write code. Abstract all possible behaviors of the system to the abstract bottom layer. These abstract classes define the methods required by all specific classes, which serve as the abstraction layer of system design. Therefore, the abstraction layer of the system does not need to be modified in any scalable scenario. At the same time, we can also dispatch other abstract classes from the abstraction layer to modify or expand the behavior of the system. Therefore, the variable part can be expanded.
It is also easy to think that it is impossible (and sometimes not suitable) to take everything into consideration during software design ). It is impossible to write everything that can be changed to the bottom layer of the abstraction. Therefore, it is difficult to fully implement the open/closed principle. This principle can only be used in certain modules, to some extent, to a certain extent. Therefore, OCP is idealistic and the ultimate goal of OOD design. Therefore, many good masters have made painstaking efforts to study the implementation of OCP. Many principles, such as the Lee principle of substitution, the principle of merging and reuse, the principle of dependency inversion, the principle of interface isolation, the abstract class, And the Demeter principle, can be considered as the implementation method of OCP.
Reference: http://blog.csdn.net/brookes/article/details/1896422
(, This is written for the moment .)
Seven Principles of object-oriented design (OOD)