Add template method example
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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": {
|
||||
|
||||
19
src/TemplatePattern/Properties/AssemblyInfo.cs
Normal file
19
src/TemplatePattern/Properties/AssemblyInfo.cs
Normal 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")]
|
||||
21
src/TemplatePattern/TemplatePattern.xproj
Normal file
21
src/TemplatePattern/TemplatePattern.xproj
Normal 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>
|
||||
15
src/TemplatePattern/TemplatePatternExamples.cs
Normal file
15
src/TemplatePattern/TemplatePatternExamples.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
src/TemplatePattern/WorkersExample/ConstructionWorker.cs
Normal file
20
src/TemplatePattern/WorkersExample/ConstructionWorker.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
19
src/TemplatePattern/WorkersExample/Manager.cs
Normal file
19
src/TemplatePattern/WorkersExample/Manager.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
23
src/TemplatePattern/WorkersExample/Student.cs
Normal file
23
src/TemplatePattern/WorkersExample/Student.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
44
src/TemplatePattern/WorkersExample/Worker.cs
Normal file
44
src/TemplatePattern/WorkersExample/Worker.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
28
src/TemplatePattern/WorkersExample/WorkersExample.cs
Normal file
28
src/TemplatePattern/WorkersExample/WorkersExample.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
13
src/TemplatePattern/project.json
Normal file
13
src/TemplatePattern/project.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"version": "1.0.0-*",
|
||||
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.0"
|
||||
},
|
||||
|
||||
"frameworks": {
|
||||
"netstandard1.6": {
|
||||
"imports": "dnxcore50"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user