Skip to content

Instantly share code, notes, and snippets.

@WilliamBZA
Created February 18, 2014 18:40
Show Gist options
  • Save WilliamBZA/9077045 to your computer and use it in GitHub Desktop.
Save WilliamBZA/9077045 to your computer and use it in GitHub Desktop.
public class Demo
{
private Dictionary<Type, Action<object>> strategies;
public Demo()
{
strategies = new Dictionary<Type, Action<object>>();
strategies.Add(typeof(OutputToConsoleWindow), value => DoSomething(value as OutputToConsoleWindow));
strategies.Add(typeof(OutputToLogFile), value => DoSomething(value as OutputToLogFile));
}
public void DoSomething<T>(ISomethingToBeDone<T> something)
{
// this method is invoked by something else
// From this method I need to invoke the concrete method calls
// e.g. Invoke DoSomething with an instance of OutputToConsoleWindow
// or invoke DoSomething with an instance of OutputToLogFile
strategies[something.GetType()](something);
}
private void DoSomething(OutputToConsoleWindow value)
{
Console.WriteLine(value.String);
}
private void DoSomething(OutputToLogFile value)
{
value._hasBeenLogged = true;
}
}
public class OutputToConsoleWindow : ISomethingToBeDone<string>
{
public string String = "haha, this is my string";
}
public class OutputToLogFile : ISomethingToBeDone<string>
{
public bool _hasBeenLogged = false;
}
public interface ISomethingToBeDone<T> { }
@joshilewis
Copy link

What does ISomethingToBeDone look like?
Also, why is public void DoSomething(ISomethingToBeDone something) generic? It doesn't look like T is used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment