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

@@ -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;

View File

@@ -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": {

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"
}
}
}