Talking about the Implementation of GOF Design Pattern in Spring Framework

Original address: Liang Guizhao's blog

Blog address: http://blog.720ui.com

Welcome to the official account: "Server Thinking". A group of people with the same frequency, grow together, improve together, and break the limitations of cognition.

Talking about the Implementation of GOF Design Pattern in Spring Framework

Before starting the text, please think about a few questions:

  • What GOF design patterns are used in your project
  • Talk about the design concept of GOF 23 design patterns
  • Talk about how to implement design patterns in the Spring framework

Suppose I am the interviewer who asks you these interview questions, how should you answer them, please think for a minute first.

OK, let's get to the point. Design pattern practice offers many enduring solutions and best practices. Here, GOF design patterns are mainly divided into three categories: creation patterns, structural patterns and behavioral patterns. Creation patterns are useful for creating object instances. Structural patterns act on the design structure of enterprise-level applications by dealing with the combination of classes or objects, thereby reducing the complexity of the application and improving the reusability and performance of the application. The intent of a behavioral pattern is the interaction between a set of objects to perform tasks that a single object cannot perform on its own. It describes class or object interactions and assignment of responsibilities.

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

Factory Method Pattern

The Spring Framework uses the factory pattern to implement the Spring container's BeanFactory and ApplicationContext interfaces. The Spring container creates beans for Spring applications based on the factory pattern and manages the lifecycle of each bean. BeanFactory and ApplicationContext are factory interfaces, and there are many implementation classes 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 the 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. Usually 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 that is used to obtain a reference to the associated bean configured by Hibernate. This is a datasource specific configuration that is used before getting the SessionFactory object. In this regard, the LocalSessionFactoryBean can be used to obtain a specific data source configuration when consistent. The reader can inject the return result of the FactoryBean's getObject() method into any other property.

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 mode

There are some features in the Spring framework that implement the builder pattern. The following are the classes based on the builder pattern in the Spring framework:

  • EmbeddedDatabaseBuilder
  • AuthenticationManagerBuilder
  • UriComponentsBuilder
  • BeanDefinitionBuilder
  • MockMvcWebClientBuilder

adapter mode

The Spring Framework uses the adapter pattern to achieve a lot of functionality. Listed below are some of the classes that use the adapter pattern in the Spring Framework:

  • JpaVendorAdapter
  • HibernateJpaVendorAdapter
  • HandlerInterceptorAdapter
  • MessageListenerAdapter
  • SpringContextResourceAdapter
  • ClassPreProcessorAgentAdapter
  • RequestMappingHandlerAdapter
  • AnnotationMethodHandlerAdapter
  • WebMvcConfigurerAdapter

bridge mode

The following is an implementation based on the bridge pattern in the Spring module:

  • ViewRendererServlet: It is a servlet bridge, mainly supporting Portlet MVC
  • Bridge mode: Spring log processing uses bridge mode

Decorator pattern

The Spring Framework uses the decorator pattern to build important functionality such as transactions, cache synchronization, and security-related tasks. Let's take a look at some of Spring's capabilities to implement this pattern:

  • Weave advice into Spring applications. It uses the CGLib proxy of the decorator pattern, which works by subclassing the target class at runtime.
  • BeanDefinitionDecorator: It enhances the definition of a bean by using custom attributes.
  • WebSocketHandlerDecorator: It is used to enhance the additional behavior of a WebSocketHandler.

Appearance Mode

In an enterprise application, if the Spring framework is used, the facade pattern is often used in the business service layer of the application, which is used to integrate all services. Readers can also apply this pattern on the DAO's persistence layer.

proxy mode

The Spring Framework uses the proxy pattern from the Spring AOP module. In Spring AOP, I can create proxies of objects to implement cross-cutting concerns. In Spring, other modules also 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 authentication and authorization functionality to be implemented in an application through the use of security filter chains. This is a highly configurable framework. Thanks to the Chain of Responsibility design pattern, we can add custom filters on the filter chain to customize functionality.

command mode

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

Interpreter mode

In the Spring Framework, the interpreter pattern is used in the Spring Expression Language (SpEL). Spring has added this new feature from Spring 3.0, and readers can use it with the Spring Framework in enterprise-level applications.

iterator pattern

The Spring Framework also extends the iterator pattern through the CompositeIterator class. This pattern is mainly used in the collection framework of the Java language to iterate through elements in order.

Observer pattern

In the Spring framework, the observer pattern is used to implement the event handling function of the ApplicationContext. Spring provides us with the ApplicationEvent class and the ApplicationListener interface to enable event handling. Any bean in a Spring application that implements the ApplicationListener interface will receive messages pushed by ApplicationEvent as the event publisher. Here, the event publisher is the subject (Subject) and the observer (Observer) of the bean that implements ApplicationListener.

After class thinking

You already know the common implementation of the GOF design pattern in the Spring framework, so can you talk about how the Spring framework implements the singleton pattern?

You are welcome to share your thoughts with me in the comment area, and you are also welcome to record your thinking process in the comment area. Thanks for reading, and if you think this article is helpful to you, please share it with more friends.

write at the end

[Server-side thinking]: Let's talk about the core technology of the server side, and discuss the project structure and practical experience of the first-line Internet. Let all the R&D personnel working alone find their own circle to communicate and discuss together. Here, we can upgrade our cognition, connect with the top technology giants, connect with excellent ways of thinking, connect with the shortest path to solve problems, connect with all excellent methods, and break the limitations of cognition.

More exciting articles, all in "Server Thinking"!

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324075933&siteId=291194637