Interviewer: I see your resume shows that you are familiar with common design patterns. Why don’t you tell me briefly which ones you are familiar with?

Candidates: Familiar factory patterns, agency patterns, template method patterns, chain of responsibility patterns, singleton patterns, package design patterns, policy patterns, etc

Candidates: the project handwritten code used more, generally template method pattern, responsibility chain pattern, strategy pattern, singleton pattern

Candidates: Factory mode, proxy mode, etc., handwritten is not much, but after all, Java backend environment in the general use of Spring, so it is familiar.

Interviewer: Why don’t you write orders by hand?

Candidates: The singleton pattern can be written in several ways

Candidates: starve, simple lazy (locking at method declaration), DCL double-checked locking (advanced lazy), static inner classes (elegant lazy), enumerations

Candidate: Hungry is when an object is initialized directly before it is used. Lazy means wait until it’s needed before you initialize it

Candidate: I’ll write it all, just the code

Interviewer: Which do you use more?

Candidates: Our projects tend to use static inner classes to implement single cases (if Spring is not available), and the code is concise and easy to read

Candidate: If you have a Spring environment, it is more convenient to manage it directly in the Spring container (Spring is singleton by default)

Candidates: Enumerations are generally used as “identifiers”, and DCL is also used in projects (and in some source code), but are generally too difficult to read and understand (:

Candidates: In general, you can use either, but I think it depends on the team’s code style (keep it consistent), even if it’s all hungry-hungry (memory is not so scarce now, I think readability is more important)

Interviewer: HMM…

Interviewer: Did you write the volatile modifier on the DCL singleton? Why is that?

Candidate: Don’t you remember when we talked about volatile? It is possible to execute instructions out of order (compiler optimization out of order, CPU cache architecture out of order, CPU native rearrangement out of order)

Candidate: The new Object code is not an atomic instruction; it consists of several steps in which instruction reordering can occur. Volatile prevents instruction reordering.

Interviewer: Can you tell me about the design patterns you used in your projects?

Candidate: Well, for example, I use the chain of responsibility mode to process requests (eliminate if else and make the project structure clearer).

Candidate: When working with common logic, the template method pattern is used for abstraction, and the specific logic is handled by different implementation classes (each message needs to be copy-validated before being sent, so copy-validated logic can be written on the abstract class)

Candidate: Less chance of writing proxy mode (since the project usually has a Spring environment, just use Spring AOP proxy)

Candidate: I have previously used AOP to encapsulate the “monitor client” in an “annotated” way (no hard-coded monitoring, just annotations)

Interviewer: Can you talk about common design patterns in Spring?

Candidates: For example, the Spring IOC container can be understood as applying the “factory pattern” (fetching objects through the ApplicationContext or BeanFactory)

Candidate: Spring objects are singletons by default, so it must be using the “singleton pattern”.

Candidates: The underlying principle of Spring AOP is the use of the “proxy pattern”, which can be either JDK dynamic proxies or CGLIB dynamic proxies

Candidate: Spring is used in many places the template method pattern, such as the transaction manager (AbstractPlatformTransactionManager), getTransaction defines the framework, many of them are made by a subclass implementation

Candidate: Spring’s event-driven model uses the “observer pattern,” which is implemented as ApplicationContextEvent and ApplicationListener

Interviewer: Well, I see…

Welcome to follow my wechat official account [Java3y] to talk about Java interview, on line interview series continue to update!

Online Interviewer – Mobile seriesTwo continuous updates a week!

Line – to – line interviewers – computer – end seriesTwo continuous updates a week!

Original is not easy!! Three times!!