Mar 22, 2013

Design pattern questions are very popular with the job interviewers, and this post covers Java strategy and factory design pattern in a tutorial style. This blog takes you through a scenario where you will be learning 4 key things.

1. Factory design pattern2. Strategy design pattern3. Applying logic to calculate variance4. Making use of the BigDecimal class for the financial calculations. The floating point variables must not be used as they can cause rounding issues.

Q. The scenario is narrated with the diagram below. It is financial application where an investor builds portfolio by buying and selling managed funds and listed securities.

The additional business rules are:

1. The managed funds that are not of type daily, must be funded from the available cash. Which means, you cannot use the proceeds you receive from selling managed funds or listed securities.

2. The daily managed funds and listed securities can be switched. This means you sell some daily managed funds to buy some listed securities.

3. The variance is calculated as total sell amount - total buy amount. The funded from cash amount also needs to be calculated to show the investor.

Q. Why use a factory design pattern? A. Factory pattern returns an instance of several subclasses (like DefaultCashVarianceCalculationStrategy, ManagedInvestmentsCashVarianceCalculationStrategy, etc), but the calling code is unaware of the actual implementation class. The calling code invokes the method on the interface for example CashVarianceCalculationStrategy and using polymorphism the relevant class and correct method gets invoked. So, as you can see, the factory pattern reduces the coupling or the dependencies between the calling code and called objects like DefaultCashVarianceCalculationStrategy, ManagedInvestmentsCashVarianceCalculationStrategy, etc. This is a very powerful and common feature in many frameworks. You do not have to create a new DefaultCashVarianceCalculationStrategy or a new ManagedInvestmentsCashVarianceCalculationStrategy on each invocation. In future, to conserve memory, you can decide to cache objects or reuse objects in your factory with no changes required to your calling code. You can also load objects in your factory based on attribute(s) read from an external properties file or some other condition. Another benefit going for the factory is that unlike calling constructors directly, factory patterns have more meaningful names like getShape(…), getInstance(…), getStrategy(…) etc, which may make calling code much clear.

Q. Why use a strategy design pattern?A. A strategy design pattern allows you to choose different algorithms at run time depending certain run time conditions like product type. Different algorithms can be isolated in different implementation classes that implement the same interface.Different client classes or invokees can execute different strategies (i.e. algorithms).

Step 6: Finally, the tester class that demonstrates the above scenario by constructing investment details and calculating the variance as shown below.

Many design patterns look similar, but the intent is different. Have a look at this post http://java-success.blogspot.com.au/2014/06/why-do-proxy-decorator-adapter-facade.html from core Java tutorial which discusses subtle differences between some of the design patterns with diagrams and examples.