Common Bits&Bytes Patterns - Strategy Pattern, Part 3 of 5 - Willy-Peter Schaub's Cave of Chamomile Simplicity

Common Bits&Bytes Patterns - Strategy Pattern, Part 3 of 5

Continued from http://dotnet.org.za/willy/archive/2008/05/16/common-bits-amp-bytes-patterns-overview-amp-template-pattern-part-2-of-5.aspx.

Strategy Pattern

Category Behavioural design pattern
Intent Define a family of algorithms, each encapsulated and interchangeable
Applicability If different variants of algorithms are needed, related classes differ only on their behaviour or if an algorithm uses data that should be hidden.
Notes The strategy pattern is a variant of the template method pattern and in the example code below, the strategy pattern is implemented using the IStrategy interface, concrete strategies A and B, with the ContextClass using the Strategy family with no knowledge of implementation.

The strategy functionality is decoupled from the context class, represented by an object and manipulated uniformly and consistently through the strategy interface.

Class Diagram

 image

Source Code Example

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace StrategyPattern
   7: {
   8:     public interface IStrategy
   9:     {
  10:         int StrategyMethod(int demoArg);
  11:     }
  12:  
  13:     public class ConcreteStrategyA : IStrategy
  14:     {
  15:         public int StrategyMethod(int demoArg)
  16:         {
  17:             return demoArg;
  18:         }
  19:     }
  20:  
  21:     public class ConcreteStratgegyB : IStrategy
  22:     {
  23:         public int StrategyMethod(int demoArg)
  24:         {
  25:             return demoArg * 2;
  26:         }
  27:     }
  28:  
  29:     public class ContextClass
  30:     {
  31:         // Helper
  32:         public bool addStrategy(IStrategy strategy)
  33:         {
  34:             if (this.numberOfStrategies < MAX_STRATEGIES)
  35:             {
  36:                 strategies[this.numberOfStrategies] = strategy;
  37:                 this.numberOfStrategies++;
  38:                 return true;
  39:             }
  40:             else return false;
  41:         }
  42:  
  43:         // Fields
  44:         private static int  MAX_STRATEGIES      = 5;
  45:         private int         numberOfStrategies  = 0;
  46:         private IStrategy[] strategies          = new IStrategy[MAX_STRATEGIES];
  47:     }
  48: }

Code Complexity Code Metrics

image

See you for part 4. ... the Iterator pattern.

Published Sunday, May 18, 2008 6:58 AM by willy

Comments

# re: Common Bits&Bytes Patterns - Strategy Pattern, Part 3 of 5

Tuesday, May 20, 2008 11:56 AM by Barend

Hi Willy,

Great post series, can you maybe add a typical example where these patterns would be used, or where you've used them before?

Barend

# Common Bits&Bytes Patterns - Iterator Pattern, Part 4 of 5

Tuesday, May 20, 2008 11:08 PM by Willy-Peter Schaub's Cave of Chamomile Simplicity

Continued from dotnet.org.za/.../common-bits-amp-bytes-patterns-strategy

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: