Advertisement
JakeJBlues

Generic Strategy Selector with Lazy Initialisation

Apr 30th, 2012
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.12 KB | None | 0 0
  1. //http://www.codedomain,
  2. // with inspiration of http://codingcraftsman.wordpress.com/2012/04/30/espirit-descalier/
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. namespace StrategySelector {
  7.    public enum InstanceType {
  8.       Singelton,
  9.       Instance
  10.    }
  11.  
  12.    public class StrategySelector<K, T> {
  13.       private readonly Dictionary<K, StrategyStorage> _lookup = new Dictionary<K, StrategyStorage>();
  14.  
  15.       public bool RegisterStrategy<TStrategyType>(K value, InstanceType instanceType) where TStrategyType : T, new() {
  16.          if (_lookup.ContainsKey(value)) {
  17.             return false;
  18.          }
  19.          if (instanceType == InstanceType.Singelton) {
  20.             _lookup[value] = new StrategyStorage {
  21.                                                       InstanceMode = instanceType,
  22.                                                       Singleton = new Lazy<T>(() => new TStrategyType())
  23.                                                    };
  24.          } else {
  25.             _lookup[value] = new StrategyStorage {
  26.                                                      InstanceMode = instanceType,
  27.                                                      StrategyType = typeof (TStrategyType)
  28.                                                   };
  29.          }
  30.          return true;
  31.       }
  32.  
  33.       public T SelectStrategy(K value) {
  34.          if (!_lookup.ContainsKey(value)) {
  35.             throw new Exception("Strategy not available");
  36.          }
  37.          if (_lookup[value].InstanceMode == InstanceType.Instance) {
  38.             return (T) Activator.CreateInstance(_lookup[value].StrategyType);
  39.          }
  40.          return _lookup[value].Singleton.Value;
  41.       }
  42.  
  43.       class StrategyStorage {
  44.         internal InstanceType InstanceMode { get; set; }
  45.         internal Lazy<T> Singleton { get; set; }
  46.         internal Type StrategyType { get; set; }
  47.       }
  48.    }        
  49. }
  50.  
  51. // Sample usage
  52. using System;
  53. using StrategySelector;
  54.  
  55. namespace ConsoleStrategy {
  56.    internal interface IStrategy {
  57.       void DoSomething();
  58.    }
  59.  
  60.    internal class MyStrategy1 : IStrategy {
  61.       public void DoSomething() {
  62.          Console.WriteLine("Strategy1");
  63.       }
  64.    }
  65.  
  66.    internal class MyStrategy2 : IStrategy {
  67.       public void DoSomething() {
  68.          Console.WriteLine("Strategy2");
  69.       }      
  70.    }
  71.  
  72.    internal class Program {
  73.      
  74.       public enum Strategies {
  75.          Strategy1,
  76.          Strategy2
  77.       };
  78.      
  79.       private static void Main(string[] args) {
  80.          var strategySelector = new StrategySelector<Strategies,IStrategy>();
  81.          strategySelector.RegisterStrategy<MyStrategy1>(Strategies.Strategy1,InstanceType.Instance);
  82.          strategySelector.RegisterStrategy<MyStrategy2>(Strategies.Strategy2,InstanceType.Singelton);
  83.          strategySelector.SelectStrategy(Strategies.Strategy1).DoSomething();
  84.          strategySelector.SelectStrategy(Strategies.Strategy2).DoSomething();
  85.          strategySelector.SelectStrategy(Strategies.Strategy1).DoSomething();
  86.          strategySelector.SelectStrategy(Strategies.Strategy2).DoSomething();
  87.          System.Console.ReadLine();
  88.       }
  89.    }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement