четвер, 6 листопада 2014 р.

Design patterns in Spring framework

 1. Creational Patterns

Singleton – this is the most common and simple design pattern. Pattern consists  with one class that is responsible for creation one instance of itself. Beans are defined in spring config xml files. Method getBean(beanName) returns one instance of predefined bean object only. Spring framework implements main idea of this pattern only. In singleton pattern class must control that only one instance of it will be created unlike in spring framework it implemented as part of bean scoping. By default all beans in spring are singletons. This can be changed by using scope attribute.

<bean class="com.blogspot.mvnblogbuild.SingletonBeanExample" id="singletonBeanExample" scope="singleton"></bean>

Prototype – duplicate is created of current object in this pattern. In spring framework this pattern implements as part of bean scoping like singleton pattern.

<bean class="com.blogspot.mvnblogbuild.PrototypeBeanExample" id="prototypeBeanExample" scope="prototype"></bean>

Factory – this pattern creates set of objects with a common parent class or interface without sharing creation logic  to the client code. Spring uses factory pattern to create objects of beans using BeanFactory child classes.

XmlWebApplicationContext and ClassPathXmlApplicationContext classes are implement ApplicationContext which is extends BeanFactory. Both those classes are factories that return user defined beans.

Builder - builder design pattern is allow to create instances of comlex class by specifying interface for creating parts of product object. Spring provides programmatic means of constructing BeanDefinitions using the builder pattern through Class "BeanDefinitionBuilder".

2. Behavioural Patterns

Template method – is used extensively to deal with boilerplate repeated code. For example JdbcTemplate, JmsTemplate, JpaTemplate.

This pattern is about defining the skeleton of the algorithm in which some of the steps of the algorithm are deferred to the sub-classes or to the data member objects.

Mediator – to reduce communications between many objects this pattern provide one class which handle all this communications. Spring framework provide DispatcherServlet class.

Strategy - in this pattern behavior of program can be changed in runtime by choosing one of the algoritms

Observer - this pattern is used when you need to notify set of objects in one to many relationship. Where observer objects waiting for events from observable object.

3. Structural Patterns

Proxy – This pattern is useful to control the access to an object and expose the same interface as that of that object. Spring framework implements this pattern in its AOP support.

4. J2EE 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 framework implements this pattern by 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.


Немає коментарів:

Дописати коментар