I have two classes which share the identical methods and I would like to switch them in code depending on requirement. If you will look at example below you will find that the same portion of code is duplicated. The CacheFile and CacheMemory classes are identical, I am sure this code can be shrinked and CacheFile and CacheMemory classes can be switched.
if (_cacheLocation == CacheLocation.File)
{
if (!CacheFile.Get(key, out value))
{
// Do lookup based on caller's logic.
context.Proceed();
value = context.ReturnValue;
CacheFile.Add(value, key, _cacheType, _expiry);
}
}
if (_cacheLocation == CacheLocation.Memory)
{
if (!CacheMemory.Get(key, out value))
{
// Do lookup based on caller's logic.
context.Proceed();
value = context.ReturnValue;
CacheMemory.Add(value, key, _cacheType, _expiry);
}
}
If you make CacheLocation and Cache Memory implement a common interface, you can use the interface instead of the individual class.
http://msdn.microsoft.com/en-us/library/ms173156.aspx