1. Brief introduction of design pattern
1.1 Classic interview questions
In the actual development process, we might use design patterns; When tracing the project source, we may also encounter design patterns in the framework source; Design patterns may also be asked during the interview. Like the following question
- Use UML classes to draw the core roles of the stereotype pattern
- What are the deep copy and shallow copy of the prototyping pattern, and write the source code for the two ways of deep copy (override the clone method for deep copy and use serialization for deep copy).
-
Where is the prototyping pattern used and source code analyzed in the Spring framework
beans.xml <bean id="id01" class="com.spring.bean.Monster" scope="prototype"/>
- The creation of a prototype bean in Spring is the application of the prototype pattern
- Code analysis +Debug source code
1.2 Seven Principles of Design Pattern
Seven common principles of design patterns: 1. Single Responsibility Principle 2. Interface Isolation Principle 3. 4. Richter substitution principle 5. Open and closed principle OCP 6. Demeter's Law 7. The principle of composite reuse
In order to have a deep understanding of the seven principles of design patterns, we need to master: 1) the core ideas of the seven design principles; 2) To be able to illustrate design principles with class diagrams; 3) Know where these rules can be reflected in the actual development of the project.
1.3 Background of several design patterns
1.3.1 Status mode
Project of financial lending platform: the order of financial lending platform includes steps such as review, release, order grab, etc. The status of the order will change with different operations. Change the order module can actually use the state mode, is also our actual work will often face the problem of table state.
Problem analysis: This kind of code is difficult to handle change. When adding a state, we need to manually add if/else, and when adding a function, we also need to judge all the states. Therefore, Diamante will become more and more bloated, difficult to maintain, once a state is not handled correctly, there will be a bug.
1.3.2 Interpreter mode
- What is the introduction interpreter design pattern?
- Draw a UML class diagram of the interpreter design pattern. Analyze what the roles are in the design pattern.
- Please explain where the interpreter design pattern is used in the Spring framework and where source-level analysis is done
- The interpreter mode is used in the Spring Framework SpelExpressionParser
- Code analysis +Debug source code + mode role analysis description
1.3.3 Singleton design pattern
How many ways are there to implement the singleton pattern? Please implement each in code, and explain the advantages and disadvantages of each implementation method. Singleton design pattern a total of 8 ways to write: hungry two, lazy three, double check, static internal class, enumeration.
2. The importance of design patterns
- In software engineering, Design Pattern is a solution proposed to various common (recurring) problems in software design. The term was introduced into computer science from the field of architectural design in the 1990s by Erich Gamma and others
- Take real work experience as an example. After a project is developed, what happens if the client proposes to add new features? . (Extensibility, using design patterns, software has good scalability)
- What would you do if the original programmer left after the project was completed? (Maintainability [readability, normalization])
- At present, the programmer threshold is getting higher and higher, the front-line IT company (big factory), will ask you in the actual project used what design pattern, how to use, what problems have been solved.
- Where are design patterns in software? Object Oriented (OO)=> Function Module [Design Patterns + Algorithms (Data Structures)]=> Framework [Use Multiple Design Patterns]=> Architecture [Server Cluster].
- Taking the time to study design patterns is essential if you want to become a qualified software engineer.
How to explain the design pattern: using the application scenario -> design pattern -> analysis principle -> analysis implementation steps (diagram)-> code implementation -> framework or project source code analysis (find the place to use)
3 Purpose of the design pattern
In the process of writing software programmer is facing from the coupling, cohesion and maintainability, scalability, reusability, flexibility and so on many aspects of challenge, design pattern is to make the program (software), has better: 1) the code reuse (i.e., the same function code, don’t have to write many times) 2) readability (i.e., Programming standardization, easy for other programmers to read and understand) 3) extensibility (that is, when we need to add new functions, it is very convenient, called maintenable) 4) reliability (that is, when we add new functions, there is no impact on the original functions) 5) make the program show high cohesion, low coupling characteristics
Share golden sentences:
- Design patterns contain the essence of object-oriented, “understand design patterns, you understand the essence of object-oriented analysis and design (OOA/D)
- Scott Mayers, in his great book Effective C++, says that the difference between C++ veterans and C++ novices is the scars on the back of their hands
Design pattern types
1. Create patterns (with emphasis on object creation) : singleton pattern, abstract factory pattern, prototype pattern, builder pattern, factory pattern. 2. Structural patterns (thinking from the perspective of software architecture) : adapter pattern, bridge pattern, decoration pattern, composition pattern, facade pattern, element pattern, agent pattern. Behavioral Patterns (from a methodological perspective) : Template Method Patterns, Command Patterns, Visitor Patterns, Iterator Patterns, Observer Patterns, Intermediator Patterns, Memo Patterns, Interpreter Patterns, State Patterns, Policy Patterns, Chain of Responsibility Patterns.
[Note] : Categories and names vary slightly from book to book