What are the design pattern available in Spring framework?

Spring Design Patterns
1. MVC - The advantage with Spring MVC is that your controllers are POJOs as opposed to being servlets. This makes for easier testing of controllers.

2. Front controller - Spring provides "DispatcherServlet" to ensure an incoming request gets dispatched to your controllers.

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

4. Singleton - Beans defined in spring config files are singletons by default.

5. Prototype - Instance type can be prototype.

6. Factory - Used for loading beans through BeanFactory and Application context.

7. Builder - Spring provides programmatic means of constructing BeanDefinitions using the builder pattern through Class "BeanDefinitionBuilder".

8. Template - Used extensively to deal with boilerplate repeated code (such as closing connections cleanly, etc..). For example JdbcTemplate.

9. Proxy - Used in AOP & Remoting.

10. DI/IOC - It is central to the whole BeanFactory/ApplicationContext stuff.

Spring Framework implemented on the layered architecture implements a good number of patterns; lets see them:

Creational Pattens
Singleton – This must be simplest but may be misused in multi-threaded programs; Spring Framework implements this pattern as part of bean scoping.
Example:
<bean id=”car” class=”com.acme.springexamples.domain.Car” scope="singleton”/>

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=”car” class=”com.acme.springexamples.domain.Car” scope="prototype”/>

Behavioural Patterns
Template – 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. 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>
Mediator – Please jump to Front Controller.
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.
Example:
<aop:config>
   <aop:aspect id="myAspect" ref="aBean">
      <aop:pointcut id="businessService"
         expression="execution(* com.xyz.myapp.service.*.*(..))"/>

      <!-- a before advice definition -->
      <aop:before pointcut-ref="businessService"
         method="doRequiredTask"/>

      <!-- an after advice definition -->
      <aop:after pointcut-ref="businessService"
         method="doRequiredTask"/>

      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref="businessService"
         returning="retVal"
         method="doRequiredTask"/>

      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref="businessService"
         throwing="ex"
         method="doRequiredTask"/>

      <!-- an around advice definition -->
      <aop:around pointcut-ref="businessService"
         method="doRequiredTask"/>
   ...
   </aop:aspect>
</aop:config>

<bean id="aBean" class="...">
...
</bean>
J2EE Patterns
Front Controller – this pattern is used to define a single point of entry for a server to handle incoming request; Spring framework implements this pattern
in its MVC module, as a servlet.
DAO Pattern – this pattern is about abstracting the database access. This patten 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>
Design Principles:
IoC/DI – Entire framework is based on this design principle.

No comments:

Post a Comment