24.08.2019

Spring, What Design Patterns

29
Spring, What Design Patterns 4,4/5 1133 reviews

What design patterns are used in Spring framework?

  1. Spring 5 Design Patterns Pdf Download
  2. Spring Design Patterns And Best Practices
thefourtheye
172k28 gold badges318 silver badges384 bronze badges

Spring 5 Design Patterns Pdf Download

Spring, What Design PatternsTonyTony

Design Pattern Details MVC Pattern MVC Design Pattern is a software design that separates the following components of a system or subsystem: Model - Data about the state of the application or its components. This tutorial will take you through step by step approach and examples using Java while learning Design Pattern concepts. Audience This reference has been prepared for the experienced developers to provide best solutions to certain problems faced during software development and for un-experienced developers to learn software design in an easy.

4,01915 gold badges51 silver badges70 bronze badges

closed as too broad by Bill the LizardSep 17 '13 at 11:10

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

9 Answers

There are loads of different design patterns used, but there are a few obvious ones:

  • Proxy - used heavily in AOP, and remoting.

  • Singleton - beans defined in spring config files are singletons by default.

  • Template method - used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate, JmsTemplate, JpaTemplate.

Update following comments: For MVC, you might want to read the MVC Reference

Some obvious patterns in use in MVC:

Spring, What Design Patterns
  • Model View Controller :-) . The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers. One thing to note is that the controller is only required to return a logical view name, and the view selection is left to a separate ViewResolver. This makes it easier to reuse controllers for different view technologies.

  • Front Controller. Spring provides DispatcherServlet to ensure an incoming request gets dispatched to your controllers.

  • View Helper - Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

toolkittoolkit
43k14 gold badges97 silver badges129 bronze badges

And of course dependency injection, or IoC (inversion of control), which is central to the whole BeanFactory/ApplicationContext stuff.

ChochosChochos

The DI thing actually is some kind of strategy pattern. Whenever you want to be some logic/implementation exchangeable you typically find an interface and an appropriate setter method on the host class to wire your custom implementation of that interface.

Oliver DrotbohmOliver Drotbohm
59.3k11 gold badges176 silver badges175 bronze badges

Spring is a collection of best-practise API patterns, you can write up a shopping list of them as long as your arm. The way that the API is designed encourages you (but doesn't force you) to follow these patterns, and half the time you follow them without knowing you are doing so.

skaffmanskaffman
349k88 gold badges745 silver badges730 bronze badges

Service Locator Pattern - ServiceLocatorFactoryBean keeps information of all the beans in the context. When client code asks for a service (bean) using name, it simply locates that bean in the context and returns it. Client code does not need to write spring related code to locate a bean.

Suman MitraSuman Mitra

Observer-Observable: it is used in ApplicationContext's event mechanism

ksevindikksevindik

Factory pattern is also used for loading beans through BeanFactory and Application context.

GurpreetGurpreet

Factory Method patter: BeanFactory for creating instance of an objectSingleton : instance type can be singleton for a contextPrototype : instance type can be prototype.Builder pattern: you can also define a method in a class who will be responsible for creating complex instance.

Praveen SharmaPraveen Sharma

Spring Design Patterns And Best Practices

Spring container generates bean objects depending on the bean scope (singleton, prototype etc..). So this looks like implementing Abstract Factory pattern. In the Spring's internal implementation, I am sure each scope should be tied to specific factory kind class.

jsrjsr

Not the answer you're looking for? Browse other questions tagged design-patternsspring or ask your own question.