Strategy Pattern - Example

Example

The following example is in Java.

// The classes that implement a concrete strategy should implement this. // The Context class uses this to call the concrete strategy. interface IStrategy { int execute(int a, int b); } // Implements the algorithm using the strategy interface class ConcreteStrategyAdd implements IStrategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyAdd's execute"); return a + b; // Do an addition with a and b } } class ConcreteStrategySubtract implements IStrategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategySubtract's execute"); return a - b; // Do a subtraction with a and b } } class ConcreteStrategyMultiply implements IStrategy { public int execute(int a, int b) { System.out.println("Called ConcreteStrategyMultiply's execute"); return a * b; // Do a multiplication with a and b } } // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object class Context { private IStrategy strategy; // Constructor public Context(IStrategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return strategy.execute(a, b); } } // Test application 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); System.out.println("Result A : " + resultA ); System.out.println("Result B : " + resultB ); System.out.println("Result C : " + resultC ); } }

Read more about this topic:  Strategy Pattern

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)