Interviewer : I see that your resume is written familiar with common design patterns. How about you briefly talk about which ones you are familiar with?
candidate : common factory model, agent model, template method model, responsibility chain model, singleton model, packaging design model, strategy model, etc. are all understood
candidate : project handwritten code is used more, generally template method mode, responsibility chain mode, strategy mode, singleton mode
Candidate : Like factory mode and proxy mode, there are not many handwritings, but after all, Spring is used in the general environment of the Java backend, so it is relatively familiar.
Interviewer : How about you write the singleton mode by hand?
candidate 161a5779c667f8: singleton mode generally has several
Candidate : Hungry, simple lazy (locked in method declaration), DCL double check and lock (advanced lazy), static inner class (elegant lazy), enum
Candidate : The so-called "hungry Chinese style" means that the object is initialized directly before it is used. The so-called "lazy man style" refers to the initialization when it is used
Candidate : Then I will write all of them, anyway, just those codes
Interviewer : Which one do you use more?
candidate 161a5779c66871: Generally, there are more our project using static internal classes (if there is no Spring environment), the code is concise and easy to read
candidate : If there is a Spring environment, it is more convenient to directly hand over to the Spring container for management (Spring is a singleton by default)
Candidate : Enumeration generally we use it as "identification", and DCL this way also has students who will write in the project (you can also see it in some source code), but the overall is too unfavorable for reading And understanding (:
Candidate : In general, you can use either one. The key I think depends on the team’s code style (just keep it consistent). Even if you use the "hungry style", there is no big problem (now Memory is not so scarce, I think readability is more important)
Interviewer : Well...
Interviewer : I think you wrote the volatile modification on the singleton code of DCL? why?
Candidate : Don’t you remember that we talked about volatile? Instructions may be executed out of order (compiler optimization causes disorder, CPU cache architecture causes disorder, CPU native rearrangement causes disorder)
candidate : when the code new Object is not an atomic instruction, it will consist of several steps. In this process, the problem of instruction rearrangement may occur, and the volatile keyword can avoid the instruction The rearrangement occurs.
Interviewer : Then you talk about the design patterns you use in the project, right?
Candidate : Well, for example, when I process requests, I will use the chain of responsibility model for processing (reducing if else and making the project structure clearer)
candidate : When processing public logic, the template method pattern will be used for abstraction, and the specific and different logic will be processed by different implementation classes (each message needs to be verified by copywriting before sending, so the copywriting can be verified The logic is written on the abstract class)
candidate : There are fewer opportunities for handwriting in proxy mode (because the project generally has a Spring environment, just use Spring's AOP proxy directly)
candidate : I have used AOP before to encapsulate the "monitoring client" and use it in the form of "annotation" (you don't need to hard-code monitoring, as long as there is an annotation)
Interviewer : Can you talk about the common design patterns of Spring?
candidate : For example, the Spring IOC container can be understood as applying the "factory pattern" (to obtain objects through ApplicationContext or BeanFactory)
candidate : Spring objects are singletons by default, so they must use the "singleton mode" (the singleton implementation in the source code uses DCL to implement the singleton)
candidate : The underlying principle of Spring AOP is to use the "agent mode", the implementation may be a JDK dynamic proxy or a CGLIB dynamic proxy
candidate : Spring uses the "template method pattern" in many places, such as the transaction manager (AbstractPlatformTransactionManager), getTransaction defines the framework, many of which are implemented by subclasses
candidate : Spring's event-driven model uses the "observer pattern", and the specific implementation is ApplicationContextEvent, ApplicationListener
Interviewer : Well, I understand...
Welcome to follow my WeChat public [161a5779c66a73 Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!
[Online Interviewer-Mobile] The series updated twice a week!
[Online Interviewer-Computer] The series updated twice a week!
Originality is not easy! ! Seek three links! !
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。