Skip to content

Commit 99cdad8

Browse files
committed
command pattern example
1 parent 2dad45d commit 99cdad8

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

CommandPattern/CommandPattern.csproj

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>

CommandPattern/Program.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
3+
namespace CommandPattern
4+
{
5+
class Program
6+
{
7+
// Receiver
8+
class Bulb
9+
{
10+
public void TurnOn()
11+
{
12+
Console.WriteLine("Bulb has been lit");
13+
}
14+
15+
public void TurnOff()
16+
{
17+
Console.WriteLine("Darkness!");
18+
}
19+
}
20+
21+
interface ICommand
22+
{
23+
void Execute();
24+
void Undo();
25+
void Redo();
26+
}
27+
28+
// Command
29+
class TurnOn : ICommand
30+
{
31+
private Bulb mBulb;
32+
33+
public TurnOn(Bulb bulb)
34+
{
35+
mBulb = bulb ?? throw new ArgumentNullException("Bulb", "Bulb cannot be null");
36+
}
37+
38+
public void Execute()
39+
{
40+
mBulb.TurnOn();
41+
}
42+
43+
public void Undo()
44+
{
45+
mBulb.TurnOff();
46+
}
47+
48+
public void Redo()
49+
{
50+
Execute();
51+
}
52+
}
53+
54+
class TurnOff : ICommand
55+
{
56+
private Bulb mBulb;
57+
58+
public TurnOff(Bulb bulb)
59+
{
60+
mBulb = bulb ?? throw new ArgumentNullException("Bulb", "Bulb cannot be null");
61+
}
62+
63+
public void Execute()
64+
{
65+
mBulb.TurnOff();
66+
}
67+
68+
public void Undo()
69+
{
70+
mBulb.TurnOn();
71+
}
72+
73+
public void Redo()
74+
{
75+
Execute();
76+
}
77+
}
78+
79+
// Invoker
80+
class RemoteControl
81+
{
82+
public void Submit(ICommand command)
83+
{
84+
command.Execute();
85+
}
86+
}
87+
88+
static void Main(string[] args)
89+
{
90+
var bulb = new Bulb();
91+
92+
var turnOn = new TurnOn(bulb);
93+
var turnOff = new TurnOff(bulb);
94+
95+
var remote = new RemoteControl();
96+
remote.Submit(turnOn); // Bulb has been lit!
97+
remote.Submit(turnOff); // Darkness!
98+
99+
Console.ReadLine();
100+
}
101+
}
102+
}

DesignPatterns.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPatter
2929
EndProject
3030
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.csproj", "{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}"
3131
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}"
33+
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3436
Debug|Any CPU = Debug|Any CPU
@@ -87,6 +89,10 @@ Global
8789
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
8890
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
8991
{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.Build.0 = Release|Any CPU
92+
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
93+
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
94+
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
95+
{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.Build.0 = Release|Any CPU
9096
EndGlobalSection
9197
GlobalSection(SolutionProperties) = preSolution
9298
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)