1. To learn the design mode, first understand what the design mode is:
1. What is the design model?
The design pattern describes a general solution to a type of common problems in the software design process.
2. What is an object-oriented design model?
The object-oriented design pattern describes the common organizational relationships between classes and communication objects in specific scenarios during the object-oriented design process.
2. Start with object-oriented:
Here we are talking about the object-oriented design model, so let's take a look at what objects are.
1. What is an object?
At the conceptual level, an object is an abstraction with certain responsibilities;
In terms of specifications, an object is a series of public interfaces that can be used by other objects.
At the language implementation level, the object encapsulates code and data (or behavior and status ).
Iii. From design principles to design patterns:
All good design patterns have something in common, and these commonalities are some good principles to be followed during code implementation.
1. Programming for interfaces is not for implementation
The customer program does not need to use the internal implementation of the object, but only needs to know the external interface of the object.
2. prioritize the use of object combinations rather than class inheritance
Class inheritance is usually "taken in white box", which damages the encapsulation of objects to some extent.
3. encapsulation changes
Encapsulation is not just information hiding. Encapsulate the changes in one place and the unchanged ones in another place. This allows the designer to modify the interface on one side of the demarcation layer without affecting the other side, so as to achieve loose coupling between layers.
4. Use the refactoring Mode
The source code is the design, and the design mode is used according to the continuous evolution of the project. A good design pattern is used as soon as it is useless. Because the design pattern cannot be applied just like an algorithm.
Several more specific principles:
1. Single Responsibility Principle
. Net class library, file class: Responsible for file paths, file storage does not exist and other information; fileinfo class: Determine the file information such as the file creation date. The two classes are implemented separately.
2. Open and closed principles
Class modules should be extensible, but cannot be modified (closed for modification and open for extension)
3. liskov replacement
Subclass must be able to replace their base class
4. Dependency inversion principle
The high-level module should not depend on the underlying module, and the two should depend on abstraction;
Abstraction should not depend on implementation details, but on abstraction.
5. Interface isolation principles
Customer programs should not be forced to rely on methods they do not need.
The design pattern generally follows these principles and should not be applied to the design pattern. The code written based on these principles may be a pattern, which is internal strength!
Important statement: This article is the notes recorded by the author when I learned from Mr. Li Jianzhong. The knowledge in this article is not original.