Skip to content

Commit 01021d4

Browse files
committed
add TemplateMethod pattern
1 parent 87ad823 commit 01021d4

File tree

5 files changed

+194
-0
lines changed

5 files changed

+194
-0
lines changed

Behavioral/TemplateMethod/Client.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace TemplateMethod {
7+
8+
class Client {
9+
10+
static void Main(string[] args) {
11+
Game monopolyGame = new Monopoly();
12+
monopolyGame.PlayOneGame(4);
13+
14+
Console.ReadKey();
15+
}
16+
}
17+
}

Behavioral/TemplateMethod/Game.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
3+
namespace TemplateMethod {
4+
5+
public abstract class Game {
6+
7+
protected int PlayerCount;
8+
9+
public abstract void InitializeGame();
10+
11+
protected abstract void MakePlay(int player);
12+
13+
protected abstract bool EndOfGame();
14+
15+
protected abstract void PrintWinner();
16+
17+
public void PlayOneGame(int playerCount) {
18+
this.PlayerCount = playerCount;
19+
20+
this.InitializeGame();
21+
22+
var j = 0;
23+
while (!this.EndOfGame()) {
24+
this.MakePlay(j);
25+
j = (j + 1) % this.PlayerCount;
26+
}
27+
this.PrintWinner();
28+
}
29+
30+
}
31+
32+
public class Monopoly : Game {
33+
34+
public override void InitializeGame() {
35+
Console.WriteLine("Initialize players");
36+
Console.WriteLine("Initialize money");
37+
}
38+
39+
protected override void MakePlay(int player) {
40+
Console.WriteLine("Process one turn of player");
41+
}
42+
43+
protected override bool EndOfGame() {
44+
Console.WriteLine("Return true if game is over, according to Monopoly rules.");
45+
return true;
46+
}
47+
48+
protected override void PrintWinner() {
49+
Console.WriteLine("Whow won ?");
50+
}
51+
52+
}
53+
54+
public class Chess : Game {
55+
56+
public override void InitializeGame() {
57+
throw new NotImplementedException();
58+
}
59+
60+
protected override void MakePlay(int player) {
61+
throw new NotImplementedException();
62+
}
63+
64+
protected override bool EndOfGame() {
65+
throw new NotImplementedException();
66+
}
67+
68+
protected override void PrintWinner() {
69+
throw new NotImplementedException();
70+
}
71+
72+
}
73+
74+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("TemplateMethod")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("TemplateMethod")]
13+
[assembly: AssemblyCopyright("Copyright © 2012")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e67506b6-7642-4531-ae0b-fec504643f74")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>TemplateMethod</RootNamespace>
12+
<AssemblyName>TemplateMethod</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Client.cs" />
47+
<Compile Include="Game.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
51+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
52+
Other similar extension points exist, see Microsoft.Common.targets.
53+
<Target Name="BeforeBuild">
54+
</Target>
55+
<Target Name="AfterBuild">
56+
</Target>
57+
-->
58+
</Project>

DesignPatterns.sln

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "State", "Behavioral\State\S
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Strategy", "Behavioral\Strategy\Strategy.csproj", "{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}"
2323
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateMethod", "Behavioral\TemplateMethod\TemplateMethod.csproj", "{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}"
25+
EndProject
2426
Global
2527
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2628
Debug|Any CPU = Debug|Any CPU
@@ -85,6 +87,12 @@ Global
8587
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|Any CPU.ActiveCfg = Release|x86
8688
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|x86.ActiveCfg = Release|x86
8789
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254}.Release|x86.Build.0 = Release|x86
90+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Debug|Any CPU.ActiveCfg = Debug|x86
91+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Debug|x86.ActiveCfg = Debug|x86
92+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Debug|x86.Build.0 = Debug|x86
93+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|Any CPU.ActiveCfg = Release|x86
94+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|x86.ActiveCfg = Release|x86
95+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5}.Release|x86.Build.0 = Release|x86
8896
EndGlobalSection
8997
GlobalSection(SolutionProperties) = preSolution
9098
HideSolutionNode = FALSE
@@ -99,5 +107,6 @@ Global
99107
{2823F21D-D474-447D-9C8B-74B29D0D9B35} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
100108
{F214B483-E8BB-477D-807F-24F176B588D5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
101109
{D728B5BD-4BE1-4B45-A0A3-08A878DAE254} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
110+
{58B97FDC-8D6B-48A6-A1D0-2EC686254CC5} = {B3C3F0E2-50B6-450B-A161-F4500F4E681D}
102111
EndGlobalSection
103112
EndGlobal

0 commit comments

Comments
 (0)