Saturday 30 August 2014

Design Patterns used in Spring Framework:


Spring Framework implemented on the layered architecture implements a good number of patterns.
1. Creational Patterns
a) Singleton – Beans defined in spring config files(xml) are only created once by default. No matter how many calls were made using getBean() method, it will always have only one bean. This is because, by default all beans in spring are singletons. This can be overridden by using Prototype bean scope.Then spring will create a new bean object for every request.
Example:
<bean id=”employee” class=”com.ranga.Employee” scope="singleton”/>
b) Factory - Spring uses factory pattern to create objects of beans using BeanFactory and Application Context reference.
// Spring uses factory pattern to create instances of the objects
BeanFactory factory=new XmlBeanFactory(new ClassPathResource("beans.xml"));
Employee employee = (Employee) factory.getBean("employee");
System.out.println(employee);

c) Prototype – This is about creating or cloning a new instance whenever required; Spring Framework implements this pattern as part of bean scoping.
Example:
<bean id=”employee” class=”com.ranga.Employee” scope="prototype”/>
d) Builder - Spring provides programmatic means of constructing BeanDefinitions using the builder pattern through Class "BeanDefinitionBuilder".
2. Behavioural Patterns
a) Template – Used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc). Spring framework implemented this pattern for variety of features like, JmsTemplate, JDBCTemplate.
Example:
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype">
            <constructor-arg ref="dataSource" />
</bean>

b) Mediator – Please jump to Front Controller.
3. Structural Patterns
a) 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.
Please refer this link.
4. J2EE Patterns
a) Front Controller - Spring provides "DispatcherServlet" to ensure an incoming request gets dispatched to your controllers.
b) DAO Pattern – this pattern is about abstracting the database access. This pattern is implemented in Spring framework in its DAO module.
Example:
<bean id="abstractJdbcDao" abstract="true" class="org.springframework.jdbc.core.support.JdbcDaoSupport">
<property name="dataSource" ref="dataSource"/>
</bean>

c) 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.
d) View Helper - Spring has a number of custom JSP tags, and velocity macros, to assist in separating code from presentation in views.

No comments:

Post a Comment

Angular Tutorial (Update to Angular 7)

As Angular 7 has just been released a few days ago. This tutorial is updated to show you how to create an Angular 7 project and the new fe...