Created
February 18, 2014 18:40
-
-
Save WilliamBZA/9077045 to your computer and use it in GitHub Desktop.
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
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> { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What does ISomethingToBeDone look like?
Also, why is public void DoSomething(ISomethingToBeDone something) generic? It doesn't look like T is used.