Tuesday, June 8, 2010

Template design pattern in action part1

In my current project we were thinking of improving the performance of our web-application. So the solution that we decided is to implement some caching mechanism in our code.

The caching was implemented something like this at back end, try to get the information from cache. If the information is not available in cache, the actual back end code will be executed and then that information will be stored in the cache. The psuedocode for the above mentioned approach was something like this




public List getPersons() {

List persons;
if(cacheManager.getData("persons") == null)  {
persons  = //Business logic to get persons;
cacheManager.putData("persons", persons);
}
return persons;
}


So this solution works perfectly fine.

Now let's try to once again look at this method (getPersons) what it is supposed to do. This method should ideally be only involved in executing business logic to get persons. The additional things that this method is doing is
1.) Trying to get the data from cache
2.) Verifying whether data is available in cache or not
3.) Updating the data in cache

Now the problem with this approach is we have to write this boiler plate caching code at every place.

If you look more deeply into this problem you will figure out actually we need a caching aspect applied to such apis. Or we can use the templating design pattern to solve this problem


EmotionsEmoticon