Interview topic: What design patterns and application scenarios are used in the Spring framework

The current question mainly examines whether developers have read the Spring source code. If they have not read the source code, they may have just heard of it. Now, where all the design patterns are used and the usage scenarios are organized as follows:

  • Simple factory: There is a factory class that dynamically determines which product class should be created based on the parameters passed in

The BeanFactory in Spring is the embodiment of the simple factory model. The Bean object is obtained by passing in a unique identifier, but whether it is created after passing in parameters or before passing in parameters depends on the specific situation.

  • Factory method: Define an interface for creating objects, let the subclass absolutely instantiate which class, and the factory method delays the instantiation of a class to its subclass.
    Insert picture description here

The bean that implements the FactoryBean interface is a type of bean called factory. Its characteristic is that when spring uses getBean() to obtain the bean, it will automatically call the bean’s getObject () method, so the bean returned is not the factory bean. But the return value of this bean.getObject() method

  • Singleton mode: ensure that a class has only one instance, and provide a global access point to access it

The singleton mode in spring completes the second half of the sentence, which provides a global access point BeanFactory , but does not control the singleton from the constructor level. This is because spring manages arbitrary java objects

  • Adapter mode: Convert the interface of a class into another interface that the customer wants. The adapter pattern allows classes that cannot work together because of interface incompatibility to work together.
    Insert picture description here

Spring defines an adapter interface HandleAdaptor , so that each Controller has a corresponding adapter implementation class, allowing the adapter to replace the Controller to execute the corresponding method, so that when extending the Controller, you only need to add an adapter class to complete the SpringMvc extension
Insert picture description here

  • Decorator pattern: dynamically add some extra responsibilities to an object. In terms of increasing functions, Decorator mode is more flexible than generating subclasses
    Insert picture description here

The decorator pattern used in Spring has two manifestations in the class name: one is that thunder contains Warpper , and the other is that the class name contains Decorator.

  • Dynamic proxy:

Section is woven into the applications running at the time, under normal circumstances, the weaving when cut, AOP container creates a proxy object as the target object, SpringAOP in this way is woven into facets.

  • Observer mode:
    Insert picture description here

Spring's event-driven model uses the observer pattern, and the commonly used Observer pattern in Spring is the implementation of EventListener.

Event : ApplicationEvent is the parent class of all event objects. ApplicationEvent inherits from jdk's EventObject, all events need to inherit ApplicationEvent, and get the event source through source.
Spring also provides us with many built-in events, ContextRefreshedEvent, ContextStartedEvent, ContextStoppedEvent, ContextClosedEvent, RequestHandledEvent.
Event listener : ApplicationListener, that is, observer, inherited from jdk's EventListener, there is only one method onApplicationEvent in this class. This method will be executed when the monitored event occurs.
Event source : ApplicationContext. ApplicationContext is the core container in Spring. In event monitoring, ApplicationContext can be used as the publisher of the event, that is, the source of the event. Because ApplicationContext inherits from ApplicationEventPublisher. The method of event publishing is defined in ApplicationEventPublisher: publishEvent(Object event)
Event management : ApplicationEventMulticaster, used for event listener registration and event broadcasting. The registration of the listener is realized through it, and its function is to broadcast the Event published by Applicationcontext to its listener list.

  • Strategy mode: define a series of algorithms, encapsulate them one by one, and make them interchangeable
    Insert picture description here

The resource access Resource interface of the Spring framework provides stronger resource access capabilities. The Spring framework itself makes extensive use of the Resource interface to access underlying resources.
Insert picture description here

Guess you like

Origin blog.csdn.net/lxn1023143182/article/details/114189490