Thursday, 24 November 2011

Strategy Pattern: Easy & Powerful

Better way to grasp patterns is find their references in existing Java API's/other frameworks. Let us start with what Strategy pattern is all about, then we will move to references part.

Lets say we have a requirement to reuse some behavior in some of the subclasses but not all.
  1. Inheritance based: Add in super class. If not required in some subclass override to empty.  This approach provides re usability of code but overriding to empty in subclasses is a maintainability nightmare.
  2. Interface based: Add an interface for that behavior. Any class which needs that behavior will implement the interface. This approach removes re usability of code and any change in that behavior must be updated in all subclasses/maintainability issue.
Strategy pattern comes on a horse to rescue !!!!
"Strategy" is representing reusable behavior and "Context" is for classes using that behavior. I will use Strategy and context very frequently now onwards.

Here we favor composition over inheritance. Concrete Strategy are our reusable behavior implementations. Any class/context which need this behavior will compose interface type as a class property. Required behavior can be injected dynamically.

Advantages:
1) Re usability of Code. No duplicability of code as in interface approach/approach 2.
2) Code maintainability is good. No need to override classes with empty implementation as in approach 1/inheritance based. Change in reusable behavior will be changed at once placed but reflected automatically in all context classes. We can also implement new concrete strategy and dynamically inject it in any of the context's.


Now we will discuss references of strategy pattern in Java API's/other frameworks.
1) Collections and Comparator: Whenever we need to sort multiple objects of a java type(context) stored in a collection we can use/implement comparator (strategy) implementation and pass it to collection.

2) Dispatcher servlet of Spring Framework: Dispatcher servlet (context) acts as a front controller for web requests and it primarily uses two strategies to process the request and serve back the response:
  •  Handler Adapter Strategy: It is used by dispatcher servlet to decide where to route the incoming request. With Spring 2.5, dispatcher servlet uses DefaultAnnotationHandlerMapping and which looks for Classes annotated with @Controller having methods with @RequestMapping annotation. Methods also specify regex as pattern which is matched with incoming request uri. Matched method is invoked. Another Default strategy is BeanNameUrlHandlerMapping
  • View Resolver Strategy: DispatcherServlet relies on View Resolver to render response. View returned by the matching method in controller can be jsp, velocity template, JSON response etc. Default one is InternalViewResolver which is used to render jsp pages. 
We can chain multiple view resolvers also which will be given chance to process the request in order if previous view resolver does not result in a view.

Strategy pattern is easy and powerful !!!