In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "what are the latest design mode interview questions in 2021". In daily operation, I believe many people have doubts about what are the latest design mode interview questions in 2021. I have consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "what are the latest design mode interview questions in 2021"! Next, please follow the small series to learn together!
1. What are design patterns?
A design pattern is a set of repeatedly used, widely known, catalogued code design experiences. Design patterns are used to reuse code, make code easier for others to understand, ensure code reliability, and reuse programs.
2. Why learn design patterns
Understand the source code: If you do not understand the design pattern to see Jdk, Spring, SpringMVC, IO and so on source code, you will be very confused, you will be unable to move
Take a look at the code of the predecessor: Do you go to a company to take over new projects? It is very likely that it is the successor of the disk. Does the development of the predecessor not use design patterns?
Write your own ideal good code: I personally anyway is like this, for my own development of the project I will be very serious, I am better than my girlfriend, the project as his son
3. Design pattern classification
4. Six Principles of Design Patterns
The Open Close Principle
Liskov Substitution Principle
Dependency Inversion principle
The Interface Segregation Principle
Demeter's Law (Least Known Principle)
Principle of Single Responsibility
5. 5.1 What is a single case?
Ensure that there is only one instance of a class and provide an access point to the global
5.2 Where are the single-case patterns used?
The counters of websites are generally implemented in single-case mode, otherwise it is difficult to synchronize.
Application log application, are generally single-case mode implementation, only one instance to operate only good, otherwise the content is not good additional display.
Multi-threaded thread pools are also generally designed in single-instance mode, because thread pools are convenient to control threads in the pool.
Windows (Task Manager) is a typical single-case mode, it can not open two
Windows (recycle bin) is also a typical single-case application. The Recycle Bin maintains only one instance throughout the system.
5.3 Advantages and disadvantages of single case
5.4 Notes on the use of single-case mode:
You cannot create a singleton using reflection mode, otherwise a new object will be instantiated.
Pay attention to thread safety when using lazy singleton mode
The construction methods of hungry singleton patterns and lazy singleton patterns are private and therefore cannot be inherited. Some singleton patterns can be inherited (such as registered patterns).
5.5 private static boolean flag = false;private Singleton() {if (flag == false) {flag = ! flag;} else {throw new RuntimeException("Singleton pattern violated! ");}}public static void main(String[] args) {}5.6 How to choose how to create a singleton
If you don't need a delayed-loading singleton, you can use enumeration or hungry, which is relatively better than hungry. If delayed loading is required, static inner classes or lazy style can be used, which is better than lazy style. It's best to use the hungry man style
5.7 Single case creation method
1. Hunger
Hungry: When the class is initialized, the object will be loaded immediately, the thread is inherently safe, and the call efficiency is high.
package com.lijie;//public class Demo1 {//class initialization, will immediately load the object, thread safe, efficient call private static Demo1 demo1 = new Demo1();private Demo1() {System.out.println ("private Demo1 construction parameter initialization");}public static Demo1 getInstance() {return demo1;}public static void main (String[] args) {Demo1 s1 = Demo1.getInstance();Demo1 s2 = Demo1.getInstance();System.out.println(s1 == s2);}}
6. Factory Mode 6.1 What is Factory Mode
It provides the best way to create objects. In factory mode, we create objects without exposing the creation logic to the client and point to newly created objects by using a common interface. Factory pattern is divided into simple factory, factory method and abstract factory pattern.
6.2 Benefits of Factory Mode
Factory pattern is our most commonly used instantiation object pattern, which is a pattern that uses factory methods instead of new operations.
The factory pattern can reduce the coupling of programs and provide great convenience for later maintenance and modification.
Unified management and control of selection implementation classes and creation objects. This decouples the caller from our implementation class.
6.3 Why learn factory design patterns
Do you know the source code of Spring, MyBatis source code, etc. If you want to learn a lot of framework source code, or you want to develop your own framework, you must first master the design pattern (factory design pattern is very very extensive)
6.4 Factory design patterns in Spring development
6.5 factory pattern classification
7. Proxy Mode 7.1 What is Proxy Mode?
By controlling access to an object through a proxy, new functionality can be processed/added before and after the object invokes a method. (i.e. P micro implementation of AO)
Agents directly cut into new code and add new functionality in business processes without modifying the original code or even the original business processes, which is also very similar to Spring's (aspect-oriented programming).
7.2 Proxy Mode Application Scenario
Spring AOP, log printing, exception handling, transaction control, permission control, etc.
7.3 Classification of agents
static proxy (statically defined proxy class)
Dynamic proxy (dynamically generated proxy class, also known as Jdk comes with dynamic proxy)
Cglib, javaassist
7.4 The difference between the three agents
1. Static agent: Simple agent pattern, which is the theoretical basis of dynamic agent. Common use in proxy mode
Jdk dynamic proxy: complete proxy using reflection. You need a top-level interface to use, and the mapper file that is commonly mybatis is a proxy.
cglib dynamic proxy: also uses reflection to complete the proxy, can directly proxy class (jdk dynamic proxy can not), use byte code technology, can not inherit the fifth class. (Need to import jar package)
7.5 Demonstrate three agents in code
7.5.1 static proxy
What is static proxy
By the programmer to create or tools to generate proxy class source code, and then compile proxy class. Static means that the bytecode file of the proxy class exists before the program runs, and the relationship between the proxy class and the delegate class is determined before running.
8. Builder Mode 8.1 What is Builder Mode
Builder pattern: separates the construction of a complex object from its representation so that the same construction process can be created differently.
The Factory Class pattern provides products that create individual classes
The builder model is to centralize and manage products with different attributes
8.2 Builder mode usage scenarios
8.3 Code Case
9. Template Method Pattern 9.1 What is a template method
Template Method Pattern: Defines the algorithmic skeleton (parent class) in an operation, deferring some steps to subclasses. Template methods allow subclasses to redefine an algorithm without changing its structure
9.2 When to use template methods
When implementing some operations, the overall steps are very fixed, but. That is, a small part of it needs to be changed. At this time, you can use the template method pattern to abstract out the easily changeable parts for subclass implementation.
9.3 Where template methods are used in application scenarios in actual development
In fact, many frameworks use template method patterns
Examples: encapsulation of database access, JUnit unit testing, calls to doGet/doPost methods in servlets, and so on
9.4 Template methods in real life
9.5 Code Implementation Template Method Pattern
10. Appearance Mode 10.1 What is Appearance Mode
Appearance pattern: Also known as facade pattern, it hides the complexity of the system and provides an interface for clients to access the system.
It adds an interface to an existing system and uses that interface to hide the complexity of the actual system.
Using the appearance pattern, it looks like an interface externally, but in fact, there are many complex interfaces that have been implemented internally.
10.2 Appearance pattern example
11. Prototype Patterns 11.1 What is Prototype Patterns?
Prototyping patterns are simply clones.
A prototype indicates that there is a template instance and that the prototype is customizable. Prototyping patterns are often used to create complex or time-consuming instances, where copying an existing instance makes the program run more efficiently.
11.2 Prototype pattern application scenarios
11.3 How to use prototype patterns
11.4 code demonstrates
12. Strategy Pattern 12.1 What is Strategy Pattern
A series of algorithms or logics or operations of the same meaning are defined, and each algorithm, logic, and operation is encapsulated, and they can be replaced with each other. (In fact, strategy patterns are very widely used in Java.)
I think it's a good idea. The complexity and difficulty of maintenance brought by else
12.2 Policy pattern application scenarios
12.3 Advantages and disadvantages of policy patterns
Advantages:
1. The algorithm can be switched freely.
Avoid using multiple conditional judgments.
3. Scalability is very good.
Disadvantages:
1. The strategy class will increase.
All policy classes need to be exposed.
12.4 code demonstrates
Analog payment modules include Weixin Pay, Alipay Pay and UnionPay Pay
Defining abstract public methods
13. Observer Mode 13.1 What is Observer Mode
Behavioral patterns focus on the interaction between objects in the system, solve the communication and cooperation between objects in the system at runtime, and further clarify the responsibilities of objects.
Observer pattern is a behavioral model, also known as publish-subscribe pattern, which defines a one-to-many dependency relationship between objects, so that when an object changes state, all dependent objects are notified and automatically updated.
13.2 Responsibilities of the model
Observer mode is primarily used for 1-to-N notifications. When an object's state changes, he needs to inform a series of objects in time to make them respond.
There are two ways to do this:
Push: Each notification is broadcast to all observers, and all observers receive it passively.
L: The observer knows what is going on, and he can decide when and what to get.
13.3 Observer mode application scenarios
Associative behavior scenarios. Note that associative behavior is separable, not a "composite" relationship. Events trigger scenarios at multiple levels.
Cross-system message exchange scenarios, such as message queues, event bus processing mechanisms.
13.4 Code Implementation Observer Pattern
At this point, the study of "What are the latest design pattern interview questions in 2021" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.