From 161e6b7c038c4f64f5540c8e52516d7c2607058a Mon Sep 17 00:00:00 2001 From: Petrutiu Mihai Date: Wed, 6 Jul 2016 09:38:55 +0300 Subject: [PATCH] Add template method example --- BehavioralPatterns.sln | 9 +++- src/BehavioralPatterns/Program.cs | 5 +++ src/BehavioralPatterns/project.json | 3 +- .../Properties/AssemblyInfo.cs | 19 ++++++++ src/TemplatePattern/TemplatePattern.xproj | 21 +++++++++ .../TemplatePatternExamples.cs | 15 +++++++ .../WorkersExample/ConstructionWorker.cs | 20 +++++++++ src/TemplatePattern/WorkersExample/Manager.cs | 19 ++++++++ src/TemplatePattern/WorkersExample/Student.cs | 23 ++++++++++ src/TemplatePattern/WorkersExample/Worker.cs | 44 +++++++++++++++++++ .../WorkersExample/WorkersExample.cs | 28 ++++++++++++ src/TemplatePattern/project.json | 13 ++++++ 12 files changed, 217 insertions(+), 2 deletions(-) create mode 100644 src/TemplatePattern/Properties/AssemblyInfo.cs create mode 100644 src/TemplatePattern/TemplatePattern.xproj create mode 100644 src/TemplatePattern/TemplatePatternExamples.cs create mode 100644 src/TemplatePattern/WorkersExample/ConstructionWorker.cs create mode 100644 src/TemplatePattern/WorkersExample/Manager.cs create mode 100644 src/TemplatePattern/WorkersExample/Student.cs create mode 100644 src/TemplatePattern/WorkersExample/Worker.cs create mode 100644 src/TemplatePattern/WorkersExample/WorkersExample.cs create mode 100644 src/TemplatePattern/project.json diff --git a/BehavioralPatterns.sln b/BehavioralPatterns.sln index f91a289..6d8a44f 100644 --- a/BehavioralPatterns.sln +++ b/BehavioralPatterns.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3820200F-354C-41E6-8F34-B301F5D621C2}" EndProject @@ -28,6 +28,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePa EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.xproj", "{01B9D869-AF89-4919-8445-79206848FB5F}" EndProject +Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TemplatePattern", "src\TemplatePattern\TemplatePattern.xproj", "{E657BF85-C23A-46DE-B837-6939D51C3321}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -70,6 +72,10 @@ Global {01B9D869-AF89-4919-8445-79206848FB5F}.Debug|Any CPU.Build.0 = Debug|Any CPU {01B9D869-AF89-4919-8445-79206848FB5F}.Release|Any CPU.ActiveCfg = Release|Any CPU {01B9D869-AF89-4919-8445-79206848FB5F}.Release|Any CPU.Build.0 = Release|Any CPU + {E657BF85-C23A-46DE-B837-6939D51C3321}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E657BF85-C23A-46DE-B837-6939D51C3321}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E657BF85-C23A-46DE-B837-6939D51C3321}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E657BF85-C23A-46DE-B837-6939D51C3321}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -84,5 +90,6 @@ Global {D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2} {0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {3820200F-354C-41E6-8F34-B301F5D621C2} {01B9D869-AF89-4919-8445-79206848FB5F} = {3820200F-354C-41E6-8F34-B301F5D621C2} + {E657BF85-C23A-46DE-B837-6939D51C3321} = {3820200F-354C-41E6-8F34-B301F5D621C2} EndGlobalSection EndGlobal diff --git a/src/BehavioralPatterns/Program.cs b/src/BehavioralPatterns/Program.cs index 216151c..c64a350 100644 --- a/src/BehavioralPatterns/Program.cs +++ b/src/BehavioralPatterns/Program.cs @@ -12,6 +12,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; +using TemplatePattern; namespace BehavioralPatterns { @@ -29,6 +30,7 @@ namespace BehavioralPatterns Console.Write("6: Memento Pattern\r\n"); Console.Write("7: State Pattern\r\n"); Console.Write("8: Strategy Pattern\r\n"); + Console.Write("9: Template method Pattern\r\n"); Console.Write("0: exit\r\n>"); var key = Console.ReadKey(); @@ -62,6 +64,9 @@ namespace BehavioralPatterns case '8': StrategyPatternExamples.Run(); break; + case '9': + TemplatePatternExamples.Run(); + break; } if (key.KeyChar == '0') break; diff --git a/src/BehavioralPatterns/project.json b/src/BehavioralPatterns/project.json index e614b94..5327460 100644 --- a/src/BehavioralPatterns/project.json +++ b/src/BehavioralPatterns/project.json @@ -19,7 +19,8 @@ }, "ObserverPattern": "1.0.0-*", "StatePattern": "1.0.0-*", - "StrategyPattern": "1.0.0-*" + "StrategyPattern": "1.0.0-*", + "TemplatePattern": "1.0.0-*" }, "frameworks": { diff --git a/src/TemplatePattern/Properties/AssemblyInfo.cs b/src/TemplatePattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a109c25 --- /dev/null +++ b/src/TemplatePattern/Properties/AssemblyInfo.cs @@ -0,0 +1,19 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Hewlett-Packard Company")] +[assembly: AssemblyProduct("TemplatePattern")] +[assembly: AssemblyTrademark("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("e657bf85-c23a-46de-b837-6939d51c3321")] diff --git a/src/TemplatePattern/TemplatePattern.xproj b/src/TemplatePattern/TemplatePattern.xproj new file mode 100644 index 0000000..de0c334 --- /dev/null +++ b/src/TemplatePattern/TemplatePattern.xproj @@ -0,0 +1,21 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + e657bf85-c23a-46de-b837-6939d51c3321 + TemplatePattern + .\obj + .\bin\ + v4.6.1 + + + + 2.0 + + + diff --git a/src/TemplatePattern/TemplatePatternExamples.cs b/src/TemplatePattern/TemplatePatternExamples.cs new file mode 100644 index 0000000..02433ae --- /dev/null +++ b/src/TemplatePattern/TemplatePatternExamples.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern +{ + public class TemplatePatternExamples + { + public static void Run() + { + WorkersExample.WorkersExample.Run(); + } + } +} diff --git a/src/TemplatePattern/WorkersExample/ConstructionWorker.cs b/src/TemplatePattern/WorkersExample/ConstructionWorker.cs new file mode 100644 index 0000000..e86047b --- /dev/null +++ b/src/TemplatePattern/WorkersExample/ConstructionWorker.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern.WorkersExample +{ + public class ConstructionWorker : Worker + { + protected override void GoToWork() + { + Console.WriteLine("Buy some beers"); + base.GoToWork(); + } + protected override void Work() + { + Console.WriteLine("If nobody is around, just look around, if boss is around work a little bit"); + } + } +} diff --git a/src/TemplatePattern/WorkersExample/Manager.cs b/src/TemplatePattern/WorkersExample/Manager.cs new file mode 100644 index 0000000..b64f3d4 --- /dev/null +++ b/src/TemplatePattern/WorkersExample/Manager.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern.WorkersExample +{ + public class Manager : Worker + { + protected override void GoToWork() + { + Console.WriteLine("Get an expensive car, and go to work"); + } + protected override void Work() + { + Console.WriteLine("Convince other people to work"); + } + } +} diff --git a/src/TemplatePattern/WorkersExample/Student.cs b/src/TemplatePattern/WorkersExample/Student.cs new file mode 100644 index 0000000..e3048a5 --- /dev/null +++ b/src/TemplatePattern/WorkersExample/Student.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern.WorkersExample +{ + public class Student : Worker + { + protected override void WakeUpAndDrinkCoffee() + { + Console.WriteLine("Not yet.."); + Console.WriteLine("Not yet..."); + Console.WriteLine("Not yet..."); + base.WakeUpAndDrinkCoffee(); + } + + protected override void Work() + { + Console.WriteLine("If session, start learning, visit 9Gag, learn again"); + } + } +} diff --git a/src/TemplatePattern/WorkersExample/Worker.cs b/src/TemplatePattern/WorkersExample/Worker.cs new file mode 100644 index 0000000..ecf72c3 --- /dev/null +++ b/src/TemplatePattern/WorkersExample/Worker.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern.WorkersExample +{ + /// + /// Framework class + /// + public abstract class Worker + { + public void StartWorking() + { + WakeUpAndDrinkCoffee(); + TakeAShower(); + GetDressed(); + GoToWork(); + Work(); + } + + protected virtual void WakeUpAndDrinkCoffee() + { + Console.WriteLine("Wake up and drink coffee normally"); + } + + protected virtual void TakeAShower() + { + Console.WriteLine("Take a shower"); + } + + protected virtual void GetDressed() + { + Console.WriteLine("Get dressed"); + } + + protected virtual void GoToWork() + { + Console.WriteLine("Go to work"); + } + + protected abstract void Work(); + } +} diff --git a/src/TemplatePattern/WorkersExample/WorkersExample.cs b/src/TemplatePattern/WorkersExample/WorkersExample.cs new file mode 100644 index 0000000..c70ad6e --- /dev/null +++ b/src/TemplatePattern/WorkersExample/WorkersExample.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace TemplatePattern.WorkersExample +{ + public class WorkersExample + { + public static void Run() + { + Console.WriteLine(); + Console.WriteLine("Manager:"); + Manager m = new Manager(); + m.StartWorking(); + + Console.WriteLine(); + Console.WriteLine("Construction worker:"); + ConstructionWorker c = new ConstructionWorker(); + c.StartWorking(); + + Console.WriteLine(); + Console.WriteLine("Student:"); + Student s = new Student(); + s.StartWorking(); + } + } +} diff --git a/src/TemplatePattern/project.json b/src/TemplatePattern/project.json new file mode 100644 index 0000000..864b9a5 --- /dev/null +++ b/src/TemplatePattern/project.json @@ -0,0 +1,13 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.6.0" + }, + + "frameworks": { + "netstandard1.6": { + "imports": "dnxcore50" + } + } +}