Skip to content

Commit 5896a58

Browse files
committed
Template method pattern example
1 parent 46e9f96 commit 5896a58

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "Strategy
4545
EndProject
4646
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePattern", "StatePattern\StatePattern.csproj", "{606D9229-082C-4A71-8DD3-1918A14B7CF2}"
4747
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateMethodPattern", "TemplateMethodPattern\TemplateMethodPattern.csproj", "{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}"
49+
EndProject
4850
Global
4951
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5052
Debug|Any CPU = Debug|Any CPU
@@ -135,6 +137,10 @@ Global
135137
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
136138
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
137139
{606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.Build.0 = Release|Any CPU
140+
{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
141+
{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Debug|Any CPU.Build.0 = Debug|Any CPU
142+
{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Release|Any CPU.ActiveCfg = Release|Any CPU
143+
{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Release|Any CPU.Build.0 = Release|Any CPU
138144
EndGlobalSection
139145
GlobalSection(SolutionProperties) = preSolution
140146
HideSolutionNode = FALSE

TemplateMethodPattern/Program.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
3+
namespace TemplateMethodPattern
4+
{
5+
abstract class Builder
6+
{
7+
// Template method
8+
public void Build()
9+
{
10+
Test();
11+
Lint();
12+
Assemble();
13+
Deploy();
14+
}
15+
16+
abstract public void Test();
17+
abstract public void Lint();
18+
abstract public void Assemble();
19+
abstract public void Deploy();
20+
}
21+
22+
class AndroidBuilder : Builder
23+
{
24+
public override void Assemble()
25+
{
26+
Console.WriteLine("Assembling the android build");
27+
}
28+
29+
public override void Deploy()
30+
{
31+
Console.WriteLine("Deploying android build to server");
32+
}
33+
34+
public override void Lint()
35+
{
36+
Console.WriteLine("Linting the android code");
37+
}
38+
39+
public override void Test()
40+
{
41+
Console.WriteLine("Running android tests");
42+
}
43+
}
44+
45+
46+
class IosBuilder : Builder
47+
{
48+
public override void Assemble()
49+
{
50+
Console.WriteLine("Assembling the ios build");
51+
}
52+
53+
public override void Deploy()
54+
{
55+
Console.WriteLine("Deploying ios build to server");
56+
}
57+
58+
public override void Lint()
59+
{
60+
Console.WriteLine("Linting the ios code");
61+
}
62+
63+
public override void Test()
64+
{
65+
Console.WriteLine("Running ios tests");
66+
}
67+
}
68+
69+
class Program
70+
{
71+
static void Main(string[] args)
72+
{
73+
var androidBuilder = new AndroidBuilder();
74+
androidBuilder.Build();
75+
76+
// Output:
77+
// Running android tests
78+
// Linting the android code
79+
// Assembling the android build
80+
// Deploying android build to server
81+
82+
var iosBuilder = new IosBuilder();
83+
iosBuilder.Build();
84+
85+
// Output:
86+
// Running ios tests
87+
// Linting the ios code
88+
// Assembling the ios build
89+
// Deploying ios build to server
90+
91+
Console.ReadLine();
92+
}
93+
}
94+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>

0 commit comments

Comments
 (0)