Thursday, May 17, 2012

SOLID Design Principles


Single Responsibility Principle – A class should be developed such a way that it should not change for more than one reason in other words a class should have one and only one reason to change.

SRP (Single Responsibility Principle) focuses on one thing one responsibility. All the methods of a class which follows the SRP principle should relate to a single purpose. It is very easy to identify the violations of SRP in a class, that class is too big and complicated with doing too many things which are unrelated.

The easiest way to fix this is to split the class based on the functionality.

Open & Close Principle – A class should be open for extension, but closed for modifications. We can use traditional object oriented methodologies like inheritance and composition for this. Following are the two ways to achieve OCP for classes
  • Making all member variables private so that other parts of the code access those using getter and setter methods.
  • Second point is to avoid typecasting the classes at runtime.
Advantage of following OCP is developer can easily change the code by without breaking the existing behavior.


Liskov Substitution Principle – Liskov Substitution Principle explains about the behavioral subtyping which is more powerful than typical subtyping of functions. LSP tells us that derived class is substitutable for its parent class.

In simple terms we can say Liskov Substitution Principle is a contract between the base and derived classes, if base is satisfied with the contract so that derived class must satisfy with the contact.


Interface Segregation Principle – ISP tells about keeping the interfaces minimal and cohesive which can be maintainable.

Problem with huge interfaces is you are forcing the classes to implement all the methods which was a burden to the developer. To fix the ISP violations break down the interfaces by applying Single Responsibility Principle which makes fine grained interfaces.

Dependency Inversion Principle – DIP is very straight forward it says whenever a class is using other classes it should depend on the corresponding interfaces not on direct classes. Dependency Inversion Principle allows the classes to be isolated which makes low impact when we change the design.

Thursday, February 16, 2012

EJB 3.1 Rocks!

I got an opportunity to explore EJB 3.1 API after a long time. In that juncture i couldn't resist my self to post some of the features of EJB 3.1 API.

Singleton capability - Singleton feature of EJB3.1 allows one session bean instance per JVM. This feature allows concurrent access and it is totally thread safe.

We can access the Singleton bean similarly how we access the stateless session bean as follows.

No-interface view - Before EJB3.1 it is necessary to have an interface for creating a bean, but EJB3.1 api allows us to create beans without an interface which eases developers life.

Asynchronous - Another nice feature of this API, which allows us to invoke session beans asynchronously

There are more features added to EJB 3.1 API.

Tuesday, February 14, 2012

Spring Data Binding

Data Binding happens in spring mvc when it submits a command object from a form. Command objects of any type used to transport data between presentation and controller layers.

When ever data transfers across these layers the core servlet (or servlet api) treats these parameters as string instead of objects of specific type.

Spring data binding enables the user to set the property values on a command object which are entered from a form.

DataBinder uses PropertyEditor (PropertyEditors basically part of Sun JavaBean API) for converting the object to string type and vice versa.

It is a better practice to explicitly register the PropertyEditor in initBinder() method for a particular controller class.

Vaadin integration with spring framework

Vaadin is a new generation framework mainly used to develop web applications. I can say Vaadin is a pure java framework where developers can use minimal or no html & javascript code to build web applications.

Add the following dependencies in pom.xml file to import all the vaadin and spring related library files into your vaadin project.
The main intention to use 'SpringApplicationSevlet' in web.xml is to integrate Spring and Vaadin frameworks. The basic purpose of this servlet is to load all the vaadin related classes.
Spring context listener used to load the spring bean configuration files.

Follow https://vaadin.com to find more information on Vaadin framework.

Tuesday, January 31, 2012

Spring 3.1 Cache Abstraction

Declarative Annotation based caching
  • @Cacheable - This is the new & important annotation that allows methods to cache the fetched data.
  • For each time when the method is triggered cache will check for the specific data.
  • Spring 3.1 caching strategy allows users to cache data based on certain conditions also called as 'Conditional caching'.
  • @CacheEvict annotation is very much useful to clear the unused data from the cache buffer.
  • Finally spring annotations cannot enable themselves automatically we have to enable it explicitly, similarly for caching also we have to enable the caching annotations by providing the following tag in our configuration file.

Sunday, January 22, 2012

Spring AOP

Aspect Oriented Programming (AOP) allows developers to think programming structure in another way. Here in my post i don't want to tell you what is the theory behind AOP, just i want to show you how easy to create aspects using spring framework.





Hope this demo helped you to understand how easy to create aspects using Spring AOP.

Friday, January 20, 2012

Why i like Spring Roo!

Spring Roo - Next generation java application generator, possible for high quality and productivity of enterprise java applications.

From Roo 1.0 the contributions of Spring Source increased very significantly so that it supported lot of technologies.

The major advantage i found in Spring Roo 1.2.1 is cloud deployment. You can easily deploy your web application into cloud foundry. It is so easy to deploy into cloud - install cloud foundry add -on, logging in into cloud and then upload your web application that's it!.