In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
GOF design pattern in the Spring framework how to achieve, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Implementation of GOF Design pattern in Spring Framework
Before you start the text, please think about a few questions:
What GOF design patterns are used in your project?
Talk about the design concept of 23 design patterns of GOF
Talk about how to implement design patterns in Spring framework
Suppose I was the interviewer who asked you these interview questions. How should you answer them? please think about them for a minute.
All right, let's get down to business. Design pattern practices provide many enduring solutions and best practices. Here, GOF design patterns are mainly divided into three categories: creation patterns, structural patterns, and behavior patterns. Creation patterns are useful for creating object instances. The structural pattern acts on the design structure of enterprise applications by dealing with the combination of classes or objects, thus reducing the complexity of the application and improving the reusability and performance of the application. The intention of a behavior 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 interaction and the allocation of responsibilities.
So, the core topic of this article is how Spring builds applications by using a large number of design patterns and good practices.
Factory method model
The Spring framework uses factory patterns to implement the BeanFactory and ApplicationContext interfaces of the Spring container. The Spring container creates a bean for Spring applications based on the factory pattern and manages the life cycle 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, FactoryBean interfaces are designed based on the abstract factory pattern. Spring provides many implementations of this interface, such as ProxyFactoryBean, JndiFactoryBean, LocalSessionFactoryBean, LocalContainerEntityManagerFactoryBean, and so on. FactoryBean helps Spring build objects that it cannot easily build by itself. This is usually used to construct complex objects with many dependencies. It can also construct highly variable 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 of the Hibernate configuration. This is a specific configuration of a data source, which is used before getting the object of the SessionFactory. In this regard, you can use LocalSessionFactoryBean to get a specific data source configuration in a consistent case. The reader can inject the result returned by the getObject () method of FactoryBean into any other property.
Singleton mode
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 some features in the Spring framework that implement the builder pattern. The following are the classes in the Spring framework 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
Bridging mode
The following is the implementation based on bridging mode in the Spring module:
ViewRendererServlet: it is a servlet bridge, mainly for Portlet MVC support
Bridge mode: Spring log processing uses bridge mode
Decorator mode
The Spring framework uses decorator patterns to build important functions such as transactions, cache synchronization, and security-related tasks. Let's take a look at some Spring capabilities that implement this pattern:
Weave notifications into the Spring application. It uses the decorator pattern's CGLib agent, which works by generating subclasses of the target class at run time.
BeanDefinitionDecorator: it enhances the definition of bean by using custom properties.
WebSocketHandlerDecorator: it is used to enhance an WebSocketHandler additional behavior.
Appearance mode
In enterprise applications, if the Spring framework is used, the facade pattern is the business services layer commonly used in the application, which is used to integrate all services. Readers can also apply this pattern to the persistence layer of DAO.
Agent mode
The Spring framework uses the proxy pattern in the Spring AOP module. In Spring AOP, the author can create proxies for objects to implement crosscutting concerns. In Spring, other modules also implement proxy patterns, such as RMI, HTTP calls for Spring, Hessian, and Burlap.
Responsibility chain model
The Spring Security project implements the chain of responsibility model. Spring Security allows authentication and authorization in an application through the use of a security filter chain. This is a highly configurable framework. Due to the use of the responsibility chain design pattern, we can add custom filters to the filter chain to customize the functionality.
Command mode
Spring MVC implements the command mode. When using the Spring framework in enterprise applications, readers will often see the command mode implemented by using command objects.
Interpreter mode
In the Spring framework, the interpreter pattern is used in 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 mode
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 the elements sequentially.
Observer mode
In the Spring framework, the Observer pattern is used to implement the event handling capabilities of ApplicationContext. Spring provides us with the ApplicationEvent class and ApplicationListener interface to enable event handling. Any bean in the Spring application that implements the ApplicationListener interface will receive a message pushed by ApplicationEvent as the event publisher. In this case, the event publisher is the Subject and the Observer of the bean that implements the ApplicationListener.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.