static void Main(string[] args) { // DYNAMIC var tp = new TextProcessor(); tp.SetOutputFormat(OutputFormat.Markdown); tp.AppendList(new[] { "foo", "bar", "baz" }); Console.WriteLine(tp); tp.Clear(); tp.SetOutputFormat(OutputFormat.Html); tp.AppendList(new[] { "foo", "bar", "baz" }); Console.WriteLine(tp); // STATIC var tpstatic = new TextProcessorStatic <MarkdownListStrategy>(); tpstatic.AppendList(new[] { "foo", "bar", "baz" }); Console.WriteLine(tpstatic); var tpstatic2 = new TextProcessorStatic <HtmlListStrategy>(); tpstatic2.AppendList(new[] { "foo", "bar", "baz" }); Console.WriteLine(tpstatic2); Console.ReadLine(); }
// Define an algorithm at a high level. // Define the interface you expect each strategy to follow. // Provide for either dynamic or static composition of strategy in the overall algorithm. public static void StaticStrategyDemo() { var tp = new TextProcessorStatic <MarkdownListStrategy>(); tp.AppendList(new[] { "foo", "bar", "baz" }); WriteLine(tp); // unfortunately we cannot assign a new one with a different ListStrategy // tp = new TextProcessorStatic<HtmlListStrategy>(); // because tp is already type of TextProcessorStatic<MarkdownListStrategy> // also we cannot make tp type of TextProcessorStatic<IListStrategy> // TextProcessorStatic<IListStrategy> tp = TextProcessorStatic<MarkdownListStrategy>(); // because that would violate the default constructor constraint var tp2 = new TextProcessorStatic <HtmlListStrategy>(); tp2.AppendList(new[] { "foo", "bar", "baz" }); WriteLine(tp2); }