Add template method example

This commit is contained in:
Petrutiu Mihai
2016-07-06 09:38:55 +03:00
parent a8aa2a5a18
commit 161e6b7c03
12 changed files with 217 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14 # Visual Studio 14
VisualStudioVersion = 14.0.25123.0 VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3820200F-354C-41E6-8F34-B301F5D621C2}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{3820200F-354C-41E6-8F34-B301F5D621C2}"
EndProject EndProject
@@ -28,6 +28,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StatePattern", "src\StatePa
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.xproj", "{01B9D869-AF89-4919-8445-79206848FB5F}" Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "StrategyPattern", "src\StrategyPattern\StrategyPattern.xproj", "{01B9D869-AF89-4919-8445-79206848FB5F}"
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TemplatePattern", "src\TemplatePattern\TemplatePattern.xproj", "{E657BF85-C23A-46DE-B837-6939D51C3321}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{01B9D869-AF89-4919-8445-79206848FB5F}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -84,5 +90,6 @@ Global
{D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2} {D48DB558-0228-4ACE-88A8-A202E5C57849} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{0B5B470D-45A4-4F6B-8DAD-D0116C40B6FB} = {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} {01B9D869-AF89-4919-8445-79206848FB5F} = {3820200F-354C-41E6-8F34-B301F5D621C2}
{E657BF85-C23A-46DE-B837-6939D51C3321} = {3820200F-354C-41E6-8F34-B301F5D621C2}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@@ -12,6 +12,7 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using TemplatePattern;
namespace BehavioralPatterns namespace BehavioralPatterns
{ {
@@ -29,6 +30,7 @@ namespace BehavioralPatterns
Console.Write("6: Memento Pattern\r\n"); Console.Write("6: Memento Pattern\r\n");
Console.Write("7: State Pattern\r\n"); Console.Write("7: State Pattern\r\n");
Console.Write("8: Strategy Pattern\r\n"); Console.Write("8: Strategy Pattern\r\n");
Console.Write("9: Template method Pattern\r\n");
Console.Write("0: exit\r\n>"); Console.Write("0: exit\r\n>");
var key = Console.ReadKey(); var key = Console.ReadKey();
@@ -62,6 +64,9 @@ namespace BehavioralPatterns
case '8': case '8':
StrategyPatternExamples.Run(); StrategyPatternExamples.Run();
break; break;
case '9':
TemplatePatternExamples.Run();
break;
} }
if (key.KeyChar == '0') if (key.KeyChar == '0')
break; break;

View File

@@ -19,7 +19,8 @@
}, },
"ObserverPattern": "1.0.0-*", "ObserverPattern": "1.0.0-*",
"StatePattern": "1.0.0-*", "StatePattern": "1.0.0-*",
"StrategyPattern": "1.0.0-*" "StrategyPattern": "1.0.0-*",
"TemplatePattern": "1.0.0-*"
}, },
"frameworks": { "frameworks": {

View File

@@ -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")]

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>e657bf85-c23a-46de-b837-6939d51c3321</ProjectGuid>
<RootNamespace>TemplatePattern</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@@ -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();
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -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");
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TemplatePattern.WorkersExample
{
/// <summary>
/// Framework class
/// </summary>
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();
}
}

View File

@@ -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();
}
}
}

View File

@@ -0,0 +1,13 @@
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.6.0"
},
"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50"
}
}
}