Strategy
Design Pattern Implementation in .Net Development
By
: Roy Saberon (MCSD)
Microsoft
Practice Manager
It's an amazing experience to
use Strategy Design Pattern in one of our project.
The one discovered is the Tax Calculation policy used
in different countries. This theory is based on the
Strategy Design Pattern by Gang of Four (GOF).
Here is the definition
of the Strategy design pattern by GOF (Gang of Four)
:
"Define a family of algorithms,
encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it."
And
here are the uses or applications by GOF :
Many related classes differ only
in their behavior. Strategies provide a way to configure
a class with one of many behaviors
You
need different variants of an algorithm. For example,
you might define algorithms reflecting different space/time
trade-offs. Strategies can be used when these variants
are implemented as a class hierarchy of algorithms
[HO87]
An algorithm uses data that clients
should not know about. Use the Strategy pattern to
avoid exposing complex, algorithm-specific data structures
A
class defines many behaviors, and these appear as
multiple conditional statements in its operations.
Instead of many conditionals, move related conditional
branches into their own Strategy class
Here
is the diagram by GOF (Gang of Four) :
Click
Image to view the full size
I had a chance to notice this design
pattern when working on a Restaurant Management System
project. And this proves to be very useful when we
have different algorithms that we can use interchangeably.
Instead of having switch or if statements,
we can encapsulate the different strategies or policies
in each own class and then instantiate the one that
is relevant.
Here is a walk through of a sample
implementation of this pattern with a Tax Calculation
project. Different countries have different Tax Calculation
policy. If our system is targeted to an international
environment it needs to have some way to load the
correct tax calculator. After all, at the end of the
day we do not want agitated Japanese clients waving
their samurais when they have been charged with special
tax on their Sake just because we used the tax calculator
intended for Singaporean restaurant. At the same time,
we also wanted the codes to be simple enough to avoid
the complexities.
In the example, that follows we
encapsulated the Tax Calculation policy for each country
on its own strategy class. Each strategy will be derived
from a base abstract class. This way, we can use each
of these strategies interchangeably without the knowledge
of the client class.
Here
is the UML diagram for our solution :
Click
Image to view the full size
Now,
for the implementation we define first the strategy
abstract class
Here
is the code in C#.Net.
public abstract class TaxCalculationStrategyBase
{
// Maintain an instance of the context class
protected OrderInfo orderInfo;
// Set the instance
of the context class.
public void SetTaxableInfo(OrderInfo orderInfoBase)
{
this.orderInfo = orderInfoBase;<input type="button"
value="Forward" onclick="history.go(+1)">
}
// Concrete strategies
will provide implementation
// for this abstract method.
public abstract void CalculateTax();
}
Then
we create a Tax Calculator concrete strategy implementation
for specific country (Japan):
public class JapanTaxCalculationStrategy
: TaxCalculationStrategyBase
{
// Default constructor
public JapanTaxCalculationStrategy()
{
}
// Implement specific
algorithm.
public override void CalculateTax()
{
// Japan uses this formula for computing
// inclusive taxes.
base.orderInfo.TaxAmount =
(base.orderInfo.OrderAmount + 10) / (100 + 10);
}
}