Tuesday, November 5, 2013

Strategy Design Pattern

Strategy Design Pattern

1.0 Overview
It is important to understand the verbal meaning of name of this pattern. It helps to draw the underneath philosophy and theory of this pattern. Following are different name of this pattern.
  1. Strategy Pattern
  2. Policy Pattern  
As name suggests; this design is to define a strategy or device a new policy. These policies or strategies can be changed at run time based on the requirement.

In software engineering; strategy or policy is also known as "algorithm". Therefore in context of software engineering; we can easily summarize this design as below

Design to define algorithms (strategies) and encapsulate (group) them  and make them interchangeable (change at run time) as per clients (users) requirement.

Note: This blog is using reference of Head First, Wikipedia and other online tutorial. 

2.0 Mini Duck Simulator
Design a duck simulator which will have different types of ducks which can swim, quack. Following are some of possible types of ducks

  1. Mallard Duck
  2. Redhead Duck

Mini Duck Simulator Program without Design Pattern
Let's first design and develop mini duck simulator program and understand the need of this pattern and problem in normal OOP approach. As per initial thought following can be design to implement this Duck simulator.
Duck.java
package patterns.strategypattern.withoutpattern;

abstract class Duck {

//Method to perform quack.
public void quack()
{
System.out.println("I am quacking.");
}

//Method to perform swim.
public void swim()
{
System.out.println("I am swimming.");
}

//Display needs to be implemented by subclass as per individual duck look.
abstract void display();


}

MallardDuck.java 
package patterns.strategypattern.withoutpattern;

public class MallardDuck extends Duck {

@Override
//Implement display behavior
void display() {
System.out.println("***I am Mallard Duck***");
}
}

ReadheadDuck.java
package patterns.strategypattern.withoutpattern;

public class ReadheadDuck extends Duck {

@Override
//Implement display behavior
void display() {
System.out.println("***I am Readhead Duck***");
}
}

MiniDuckSimulator.java
package patterns.strategypattern.withoutpattern;

public class MiniDuckSimulator {

public static void main(String[] args) {

//Show Mallard duck
Duck mallard= new MallardDuck();
mallard.display();
mallard.swim();
mallard.quack();
//Show Redhead duck
Duck readhead= new ReadheadDuck();
readhead.display();
readhead.swim();
readhead.quack();
}

}
Output:

***I am Mallard Duck***
I am swimming.
I am quacking.
***I am Readhead Duck***
I am flying.
I am quacking.

Mini Duck Simulator Change
Add fly behavior in few ducks. 

To add fly behavior in ducks; easiest approach as per above design will be to add fly() method in Duck superclass as below.

//Method to perform fly.
public void fly()
{
System.out.println("I am flying.");
}

Issues by this change:
The ducks which was not suppose to fly is also started fly. For example RubberDucks (just taken as example) also starting flying.
Approach to fix flying issue:
Override "fly" method from Duck superclass and implement it in subclass which doesn't need to fly as do nothing.
Issue in above approach:
Code duplicity in all the classes which doesn't need fly.









No comments:

Post a Comment