Applying strategy pattern for conditional logic
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sriram_Kanala
Posted On:   Monday, September 1, 2003 02:11 AM

I am new to using strategy pattern. My application has the following logic: if (a) {   // do something   if (b) {     // do something   } else {     // do something else   } } else {   if (c) {     // do something   } else {     // do something else   } } I am wondering how to avoid this kind of if..else statements. Can I use strategy patten in this case? And how? Can someone please guide me.    More>>

I am new to using strategy pattern.



My application has the following logic:



if (a) {

  // do something

  if (b) {

    // do something

  } else {

    // do something else

  }

} else {

  if (c) {

    // do something

  } else {

    // do something else

  }

}



I am wondering how to avoid this kind of if..else statements. Can I use strategy patten in this case? And how? Can someone please guide me.

   <<Less

Re: Applying strategy pattern for conditional logic

Posted By:   chris_moran  
Posted On:   Saturday, October 4, 2003 07:49 AM



There are always other ways to do anything. However there are always tradeoffs. Doing what you are doing above is intuitive. (Relatively speaking of course). In other words, it is fairly simple to understand and debug if problems arise. There is another way to perform what you want.



  • Define a command interface.
  • Implement a command for each action.
  • Create a typesafe enum
  • Put each command in its corresponding typesafe enum
  • Modify the executing method to take as a parameter a variable of your typesafe enum type
  • Execute the method on the typesafe enum that was passed in.


For example:



Command Interface
=================
public interface Command {
public void execute();
}

Implementations
===============
public class HirePerson implements Command {
/* Other code */
public void execute() {
System.out.println("You're HIRED!");
}
}

public class FirePerson implements Command {
/* Other code */
public void execute() {
System.out.println("You're FIRED!");
}
}

Typesafe Enum
=============
public final class CommandType {
private Command command = null;
private CommandType(Command _command) {
command = _command;
}
public void execute() {
command.execute();
}
public static final CommandType HIRE = new CommandType(new HirePerson());
public static final CommandType FIRE = new CommandType(new FirePerson());
}

Usage
=====

public class Test {
public static void main(String[] data) {
doThing(CommandType.HIRE);
doThing(CommandType.FIRE);
}

public static void doThing(CommandType type) {
type.execute();
}
}


Of course this example is extremely trivial. In fact, the whole typesafe enum can be completely omitted in favor of just interfaces. But in complex examples, the typesafe enum can perform more logic than this trivial example. The downside of this approach is that it creates many class files and is not the simplest to debug.

Re: Applying strategy pattern for conditional logic

Posted By:   Anonymous  
Posted On:   Tuesday, September 2, 2003 04:31 AM

Even when using the Strategy pattern, you still need to somehow decide which strategy to use. So wether this pattern would help depends heavily on what your conditionals a, b and c look like. The pattern is actually most usefull if otherwise you needed to replicate the conditional logic at different places.

Can you show us some more concrete code?

Re: Applying strategy pattern for conditional logic

Posted By:   Stephen_McConnell  
Posted On:   Monday, September 1, 2003 05:23 AM

Usually, if you define an interface with a particular method on it that is overriden by the implementing class then you are using the "strategy" design pattern.


Your code would go something like this.



public interface GoLeftYoungMan{
boolean goLeft(int distance);
}

public class Car implements GoLeftYoungMan{
...... bunches of Car code....

public boolean goLeft(int distance){
boolean madeIt = false;
/// code to turn the wheel
/// code to turn the car
/// code to move the car so much "distance"
return madeIt;
}
}

public class Dog implements GoLeftYoungMan{
..... bunches of Dog code .....

public boolean goLeft(int distance){
boolean madeIt = false;
/// Code to look both ways.
/// Code to see the nearest fire hydrant
/// Code to move the the back end around.
/// Code to change to front end direction
/// Code to go the distance
madeIt = true;


return madeIt;
}

}

public class TeenAger implements GoLeftYoungMan{
.... small amount of teenager code ....

public boolean goLeft(int distance){
boolean madeIt = false;
// Code to turn to the right.....

return madeIt;
}




public class MoverClass{


public boolean negotiatePath(GoLeftYoungMan negotiate){
.... Code to calculate distance...

return negotiate.goLeft(distance);


}




}



So, you see that you pass in the Implementing class by casting to the interface.... Then, all you do is execute the over-ridden method. The concrete implementation of the method or methods is what determines which "Strategy" is being used.


Hope this helps.


Stephen McConnell

About | Sitemap | Contact