KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
When would the Strategy Pattern be used? I see client code snippets like this: class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyAdd()); int resultA = context.executeStrategy(3,4); context = new Context(new ConcreteStrategySubtract()); int resultB = context.executeStrategy(3,4); context = new Context(new ConcreteStrategyMultiply()); int resultC = context.executeStrategy(3,4); } } and it looks like you could just refactor it to this: class StrategyExample { public static void main(String[] args) { // Three contexts following different strategies int resultA =new ConcreteStrategyAdd().execute(3,4); int resultB =new ConcreteStrategySubtract().execute(3,4); int resultC =new ConcreteStrategyMultiply().execute(3,4); } } The first section of code was taken directly from the wikipedia page. One big difference is the context goes away, but it wasn't doing anything in the example anyway. Maybe someone has a better example where Strategy makes sense. I usually like design patterns but this one seems to add complexity without adding usefulness.
Tags (comma-separated)
Save Edits
Cancel