preface
Design pattern is a very important skill in daily work. It can be used to reconstruct the overall architecture code, submit code reusability, extensibility and reduce code redundancy. This is a skill every Java engineer must have! Today, xiaobian mainly talks about one of the design patterns, the strategy pattern. Xiaobian will take you step by step to analyze the strategy pattern through cases and interviews.
Java Oop, Java Collections containers, Java exceptions, concurrent programming, Java reflection, Java serialization, JVM, Redis, Spring MVC, MyBatis, MySQL database, messaging middleware MQ, Dubbo, Linux, ZooKeeper, distributed & data structure and algorithm, etc. 25 thematic technical points, are all small editor in each big factory summary of the interview real questions, there have been many fans with this PDF to win many big factory offer. Today, here is a summary to share to everyone! [Finished]
The full version of the Java interview questions address: 2021 latest interview questions collection collection.
The serial number | project | content | link |
---|---|---|---|
1 | The middleware | Java Middleware (2021) | Juejin. Cn/post / 694870… |
2 | Micro service | Java Microservices (2021) | Juejin. Cn/post / 694906… |
3 | Concurrent programming | Concurrent Programming in Java (2021 latest Edition) | Juejin. Cn/post / 695053… |
4 | Java based | Java Basics (2021) | Juejin. Cn/post / 695062… |
5 | Spring Boot | Spring Boot Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695137… |
6 | Redis | Redis Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695166… |
7 | Spring MVC | Spring MVC (2021) | Juejin. Cn/post / 695166… |
8 | Spring Cloud | Spring Cloud Interview Questions (2021) | Juejin. Cn/post / 695245… |
9 | MySQL optimization | MySQL optimize interview questions (2021 latest edition) | Juejin. Cn/post / 695246… |
10 | JVM | JVM Performance Tuning Interview questions (2021 Latest Edition) | Juejin. Cn/post / 695246… |
11 | Linux | Linux Interview Questions (2021 latest edition) | Juejin. Cn/post / 695287… |
12 | Mybatis | Mybatis (2021 latest Edition) | Juejin. Cn/post / 695287… |
13 | Network programming | TCP, UDP, Socket, Http Network programming interview (2021 latest edition) | Juejin. Cn/post / 695287… |
14 | Design patterns | Design Mode Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695544… |
15 | Big data | 100 Big Data Interview Questions (2021 latest edition) | Juejin. Cn/post / 695544… |
16 | Tomcat | Tomcat Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695570… |
17 | multithreading | Multithreaded Interview Questions (2021 Latest edition) | Juejin. Cn/editor/draf… |
18 | Nginx | Nginx_BIO_NIO_AIO interview Questions (2021 Latest edition) | Juejin. Cn/editor/draf… |
19 | memcache | Memcache Interview Questions (2021 latest edition) | Juejin. Cn/post / 695608… |
20 | Java exception | Java Exception Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695644… |
21 | The Java virtual machine | Java Virtual Machine Interview (2021 latest edition) | Juejin. Cn/post / 695658… |
22 | Java collection | Java Set Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695684… |
23 | Git Git | Git Command (2021) | Juejin. Cn/post / 695692… |
24 | Elasticsearch | Elasticsearch (2021 Latest Edition) | Juejin. Cn/post / 695840… |
25 | Dubbo | Dubbo Interview Questions (2021 Latest edition) | Juejin. Cn/post / 695842… |
1. What are design patterns
A design pattern is a set of repeated, familiar, catalogued code design lessons. Design patterns are used to make code reusable, to make it easier for others to understand, to ensure code reliability, and program reuse.
2. Why study design patterns
- Read the source code: If you don’t know the design pattern and look at the source code for Jdk, Spring, SpringMVC, IO, etc., you will be confused, you will be unable to do anything
- Take a look at the code of your predecessors: Is every new project you go to a company for? It is likely to be a successor, the previous development did not use design patterns?
- Write your own ideal of good code: PERSONALLY, I take my own projects seriously, treat them better than my girlfriend, and treat them like my own son
3. Classification of design patterns
4. Six principles of design patterns
- Open Close Principle
- Liskov Substitution Principle
- Dependence Inversion Principle
- Interface Segregation Principle
- The Demeter Principle
- Principle of Single Responsibility
5. Singleton mode
5.1 What is a Singleton
Ensure that there is only one instance of a class and provide a global access point to that class
5.2 Where is the singleton pattern used
- The counter of the website is generally realized by singleton mode, otherwise it is difficult to synchronize.
- The logging application of the application program is generally implemented in singleton mode, only one instance to operate, otherwise the content is not good to append display.
- Multithreaded thread pools are also designed in a singleton pattern, because thread pools are designed to facilitate control of threads in the pool
- Windows (Task Manager) is a very typical singleton, it can’t open two
- Windows recycle bin is also a typical singleton application. The recycle bin maintains only one instance during the entire system operation.
5.3 Advantages and disadvantages of singleton
5.4 Precautions for Using singleton Mode
- You cannot use reflection mode to create singletons, otherwise a new object will be instantiated
- Be aware of thread-safety issues when using lazy singletons
- Both the hungry singleton and lazy singleton constructors are private and therefore cannot be inherited. Some singleton patterns can be inherited (e.g., the registry pattern)
5.5 Singletons Prevent reflection vulnerability attacks
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) { }Copy the code
5.6 How Do I Select the Singleton Creation Mode
If you don’t need lazy loading singletons, you can use enumerations or hangry, which are relatively more enumerative than hangry. If you need to delay loading, you can use static inner classes or lazy, which are relatively better than lazy Korean. It’s best to use hungry
5.7 Singleton Creation Mode
1. The hungry
- Hungry: When the class is initialized, the object is loaded immediately, and the thread is inherently safe and efficient to call.
package com.lijie; Private static Demo1 Demo1 = new Demo1(); private static Demo1 Demo1 = new Demo1(); Private Demo1() {system.out.println (" Private Demo1 constructor 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); }}Copy the code
6. Factory mode
6.1 What is factory Mode
It provides an optimal way to create objects. In factory mode, we create objects without exposing the creation logic to the client, and by using a common interface to point to the newly created objects. The separation of creator and caller is realized, and the factory pattern is divided into simple factory, factory method and abstract factory pattern
6.2 Benefits of factory mode
- Factory mode is the most commonly used instantiation object mode, which uses factory methods instead of new operations.
- Using factory mode can reduce the coupling of the program, which provides great convenience for the later maintenance and modification.
- You will select implementation classes and create objects for unified management and control. This decouples the caller from our implementation class.
6.3 Why learn factory design mode
If you want to learn the source of many frameworks, or you want to develop their own framework, you must first master the design mode (factory design mode is used very, very widely).
6.4 Factory design patterns in Spring development
6.5 Factory Mode classification
7. Proxy mode
7.1 What is the Proxy Mode
- Proxies control access to an object so that new functionality can be handled/added before and after the object calls a method. (That is, the P micro implementation of AO)
- Agents insert new code and add new functionality directly into the business process without modifying the original code or even the original business process, similar to Spring’s (aspect oriented programming)
7.2 Application Scenarios of the Proxy Mode
Spring AOP, log printing, exception handling, transaction control, permission control, etc
7.3 Classification of agents
- Static proxy (statically defining proxy classes)
- Dynamic proxy (dynamically generated proxy class, also known as Jdk built-in dynamic proxy)
- Cglib, JavaAssist (Bytecode Manipulation library)
7.4 Differences between the three agents
1. Static proxy: simple proxy mode is the theoretical basis of dynamic proxy. Commonly used in proxy mode
- JDK Dynamic Proxy: Use reflection to complete the proxy. Need to have a top-level interface to use, common is mybatis mapper file is proxy.
- Cglib dynamic proxy: Also use reflection complete proxy, can directly proxy classes (JDK dynamic proxy not), using bytecode technology, cannot inherit from fifinal classes. (Need to import jar package)
7.5 Demonstrate the three agents in code
7.5.1 Static Proxy
What is a static proxy
The source code of the proxy class is created by the programmer or generated by the tool, and then compiled by the 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 the program runs.
8. Builder mode
8.1 What is the Builder model
- The Builder pattern: Separates the construction of a complex object from its representation so that the same construction process can create different ways to create it.
- The factory class pattern provides products that create individual classes
- The Builder model is to centralize and manage products with different attributes
8.2 Usage scenario of builder pattern
8.3 Code Cases
9. Template method pattern
9.1 What is the Template Method
Template method pattern: Defines an algorithm skeleton (superclass) in an operation, deferring some steps to subclasses. The template method allows subclasses to redefine an algorithm without changing its structure
9.2 When to Use the Template Method
When you implement some operations, the overall steps are pretty fixed, but. In this case, the template method pattern can be used to abstract out the easily changeable parts for subclasses to implement.
9.3 Where is the template method used in actual Development Application Scenarios
- In fact, many frameworks use the template method pattern
- Examples include encapsulation of database access, Junit unit testing, invocation of doGet/doPost methods in servlets, and more
9.4 Template methods in real life
9.5 Code implements the template method pattern
10. Appearance mode
10.1 What is Appearance Mode
- Facade mode: Also known as facade mode, hides the complexity of the system and provides an interface through which clients can access the system.
- It adds an interface to an existing system that hides the complexity of the actual system.
- With the appearance pattern, it looks like an interface on the outside, but inside it there are many complex interfaces that have been implemented
10.2 Appearance Pattern examples
11. Prototype mode
11.1 What is prototyping
- Prototyping patterns are simply clones
- A stereotype indicates that there is a sample instance and that the stereotype is customizable. The prototype pattern is often used to create complex or time-consuming examples where copying an existing instance makes the program run more efficiently.
11.2 Application Scenarios of prototype Mode
11.3 Usage of prototype pattern
11.4 Code Demo
12. Strategic mode
12.1 What is the Policy Mode
- Defines a series of algorithms or logic or operations of the same meaning, and encapsulates each algorithm, logic, and operation, and makes them interchangeable. (Policy patterns are very, very widely used in Java.)
- I think it’s mainly to simplify if… Else is complex and difficult to maintain
12.2 Application Scenarios of Policy Mode
12.3 Advantages and disadvantages of the policy mode
Advantages:
1. The algorithm can be switched freely.
2. Avoid using multiple conditional judgments.
3. Very good scalability.
Disadvantages:
1. Policy classes will increase.
2. All policy classes need to be exposed.
12.4 Code demo
Analog payment modules include wechat Pay, Alipay pay and UnionPay
- Define abstract public methods
13. Observer mode
13.1 What is the Observer mode
- First, what is the behavioral model? The behavioral model focuses on the interaction between objects in the system, solves the communication and cooperation between objects in the running time of the system, and further clarifies the responsibilities of objects.
- The observer model, also known as the publisk-subscribe model, is a behavioral model that defines a one-to-many dependency between objects so that when an object changes state, all dependent objects are notified and updated automatically.
13.2 Responsibilities of the mode
The observer mode is mainly used for 1-to-N notifications. When an object’s state changes, he needs to inform a series of objects so that they can respond.
There are two ways to implement this:
- Push: Notifications are broadcast to all observers each time, and all observers can only passively receive them.
- Ra: As long as the observer is aware of the situation, he or she can decide when to get the content and what to get.
13.3 Application Scenario of Observer Mode
- Associative behavior scenarios, it is important to note that associative behavior is separable, not a “composite” relationship. Multi-level event triggering scenario.
- Cross-system message exchange scenarios, such as message queues, event bus processing mechanisms.