The author | Liang Guizhao, slightly technology architects


Before you begin the body, please consider a few questions:
  • What GOF design patterns are used in your project
  • Talk about the design concept of GOF’s 23 design modes
  • How do you implement design patterns in the Spring framework

If I were the interviewer and asked you these interview questions, think for a minute about how you would respond.


All right, let’s get down to business. Design pattern practices offer many enduring solutions and best practices. Here, GOF design patterns fall into three main categories: creation patterns, structural patterns, and behavior patterns. The create pattern is useful for creating object instances. Structural patterns act on the design structure of enterprise applications by dealing with the combination of classes or objects, thus reducing the complexity of applications and improving the reusability and performance of applications. The intent of a behavior pattern is an interaction between a group of objects to perform tasks that a single object cannot perform on its own. It describes class or object interactions and the assignment of responsibilities.

So, the core topic of this article is how Spring builds applications using a number of design patterns and good practices.


Factory method pattern

The Spring framework uses the factory pattern to implement the BeanFactory and ApplicationContext interfaces of the Spring container. The Spring container creates beans for Spring applications based on the factory pattern and manages the life cycle of each bean. BeanFactory and ApplicationContext are factory interfaces, and many implementation classes exist in Spring. The getBean() method is the factory method of the corresponding bean.


Abstract Factory pattern

In the Spring framework, the FactoryBean interface is designed based on an abstract factory pattern. Spring provides many implementations of this interface, Such as ProxyFactoryBean, JndiFactoryBean, LocalSessionFactoryBean, LocalContainerEntityManagerFactoryBean, etc. Factorybeans help Spring build objects that it cannot easily build on its own. Typically this is used to construct complex objects with many dependencies. It can also construct highly volatile logic based on configuration. For example, in the Spring framework, LocalSessionFactoryBean is an implementation of FactoryBean, which is used to get a reference to Hibernate configured associated beans. This is a specific configuration of the data source that is used before the object of the SessionFactory is obtained. This can be done consistently with LocalSessionFactoryBean to get specific data source configuration. The reader can inject the result returned by the getObject() method of a FactoryBean into any other property.


The singleton pattern

The Spring framework provides a singleton bean to implement the singleton pattern. It is similar to the singleton pattern, but it is not exactly the same as the singleton pattern in Java.


Builder model

There are several features in the Spring framework that implement the Builder pattern. The following classes in the Spring framework are based on the Builder pattern:
  • EmbeddedDatabaseBuilder
  • AuthenticationManagerBuilder
  • UriComponentsBuilder
  • BeanDefinitionBuilder
  • MockMvcWebClientBuilder


Adapter mode

The Spring framework uses the adapter pattern to implement many functions. Here are some classes that use the adapter pattern in the Spring framework:
  • JpaVendorAdapter
  • HibernateJpaVendorAdapter
  • HandlerInterceptorAdapter
  • MessageListenerAdapter
  • SpringContextResourceAdapter
  • ClassPreProcessorAgentAdapter
  • RequestMappingHandlerAdapter
  • AnnotationMethodHandlerAdapter
  • WebMvcConfigurerAdapter


The bridge model

The following is an implementation based on the bridge pattern in the Spring module:
  • ViewRendererServlet: This is a servlet bridge that primarily supports Portlet MVC
  • Bridge mode: Spring logging uses bridge mode


Decorator mode

The Spring framework uses the decorator pattern to build important functions such as transactions, cache synchronization, and security-related tasks. Let’s take a look at some Spring features that implement this pattern:
  • Weave notifications into the Spring application. It uses the Decorator pattern’s CGLib proxy, which works by generating subclasses of the target class at run time.
  • BeanDefinitionDecorator: This enhances the definition of beans by using custom attributes.
  • WebSocketHandlerDecorator: it is used to enhance a WebSocketHandler additional behavior.


The appearance model

In enterprise applications, if the Spring framework is used, the facade pattern is the business services layer commonly used in applications to integrate all services. Readers can also apply this pattern on the persistence layer of daOs.


The proxy pattern

The Spring framework uses the proxy pattern in the Spring AOP module. In Spring AOP, authors can create proxies for objects to implement crosscutting concerns. In Spring, other modules implement the proxy pattern, such as RMI, Spring’s HTTP calls, Hessian, and Burlap.


Chain of Responsibility model

The Spring Security project implements the chain of responsibility pattern. Spring Security allows you to implement authentication and authorization capabilities in your applications by using chains of Security filters. This is a highly configurable framework. Using the chain of responsibility design pattern, we can add custom filters to the filter chain to customize functionality.


Command mode

Spring MVC implements the command pattern. Using the Spring framework in enterprise applications, readers often see command patterns implemented using command objects.

Interpreter mode

In the Spring framework, the interpreter pattern is used in the Spring Expression Language (SpEL). Spring adds this new feature from Spring 3.0, and readers can use it in enterprise applications through the Spring framework.


Iterator pattern

The Spring framework also extends the iterator pattern through the CompositeIterator class. This pattern is used primarily in the Java language’s collections framework to access elements iteratively in order.


Observer model

In the Spring framework, the observer pattern is used to implement the event handling capabilities of the ApplicationContext. Spring provides the ApplicationEvent class and the ApplicationListener interface to enable event handling. Any bean in a Spring application that implements the ApplicationListener interface receives a message pushed by ApplicationEvent as an event publisher. In this case, the event publisher is the Subject and Observer of the bean that implements the ApplicationListener.


At the end of the article think

Now that you know how the GOF design pattern is commonly implemented in the Spring framework, can you consider how the Spring framework implements the singleton pattern?

If you find this article helpful, please feel free to share it with more friends.



Wechat official account
  • V V technology | IntelliJ IDEA in common plug-in, let the efficiency become a habit
  • Technology of dry goods | million level commodity data synchronization, real-time query results in seconds
  • Slightly recruitment | Q2 ReZhao post, recommend that 100% bonus


Follow the public account for more technical dry goods and recruitment information