Created
March 21, 2012 18:23
-
-
Save hyeomans/2150756 to your computer and use it in GitHub Desktop.
Strategy Pattern with BitRate Conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace StrategyPattern | |
{ | |
public enum UnitOfBitRate | |
{ | |
Gbps, | |
Mbps, | |
Kbps, | |
Bps | |
} | |
public abstract class UnitOfBitRateBase | |
{ | |
protected int Factor = 1000; | |
public abstract int DoAlgorithm(ref int bitRateValue); | |
} | |
public class BpsBitRateBase : UnitOfBitRateBase | |
{ | |
public override int DoAlgorithm(ref int bitRateValue) | |
{ | |
return bitRateValue; | |
} | |
} | |
public class KbpsBitRateBase : UnitOfBitRateBase | |
{ | |
public override int DoAlgorithm(ref int bitRateValue) | |
{ | |
return bitRateValue /= Factor; | |
} | |
} | |
public class MbpsBitRateBase : UnitOfBitRateBase | |
{ | |
public override int DoAlgorithm(ref int bitRateValue) | |
{ | |
return bitRateValue /= (Factor * Factor); | |
} | |
} | |
public class GbpsBitRateBase : UnitOfBitRateBase | |
{ | |
public override int DoAlgorithm(ref int bitRateValue) | |
{ | |
return bitRateValue /= (Factor * Factor * Factor); | |
} | |
} | |
public class BitRateContext | |
{ | |
private static Dictionary<UnitOfBitRate, UnitOfBitRateBase> _strategies = | |
new Dictionary<UnitOfBitRate, UnitOfBitRateBase>(); | |
static BitRateContext() | |
{ | |
_strategies.Add(UnitOfBitRate.Bps, new BpsBitRateBase()); | |
_strategies.Add(UnitOfBitRate.Kbps, new KbpsBitRateBase()); | |
_strategies.Add(UnitOfBitRate.Gbps, new GbpsBitRateBase()); | |
_strategies.Add(UnitOfBitRate.Mbps, new MbpsBitRateBase()); | |
} | |
public static int DoAlgorithm(UnitOfBitRate rate, ref int bitRate) | |
{ | |
return _strategies[rate].DoAlgorithm(ref bitRate); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
UnitOfBitRate rate = UnitOfBitRate.Gbps; | |
int unitOfBitRate = 100000; | |
Console.WriteLine(BitRateContext.DoAlgorithm(rate, ref unitOfBitRate)); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment