Posts

Showing posts from November, 2009

Strategy Design Pattern by example

This post explains Strategy design pattern using an example. When to Use ? – Its an alternative to Template Design Pattern. Use this pattern when template methods implementation as well as steps in which they need to execute are dynamic. Its like no of steps in algorithm vary or the class that implements the steps needs as independent inheritance hierarchy . How to use ? – Implement an interface defining the individual steps. Define a class that implements this interface ( individual steps) , this class is not forced to inherit from an abstract template super class. Main algorithm implementation class uses this interface implementation for each of algorithm steps execution An example – Same example as Template Method Design pattern is being used, Process order. Interface - package strategypattern; import java.util.List; import templatemethod.Order; public interface ProcessOrderHelper {     public  boolean  validateCustomer(String custName...

Template Method Design Pattern by Example

This post explains Template Design Pattern using a simple example. When to Use ?   – When you know the steps of an algorithm and order in which they should be performed, but don’t know how to perform all of the steps or implementations of the steps vary from user to user. This pattern is the solution for algorithm implementation where invocation order of steps is fixed, but steps implementation may vary for example getting data from database or file system etc. How to Use ? – Encapsulate all individual steps steps we don’t know how to perform as abstract methods in an super abstract class and provide a final method that invokes steps in correct order. Concrete subclasses of this super abstract class implement the abstract methods that performs individual steps. They concept is that super class controls invocation order of the steps and leave implementation of these steps up to end user.       When implementing the Template Method pattern, the abstrac...