Skip to content

Commit ce127e1

Browse files
committed
Add command + update decorator pattern.
1 parent 2b6ad35 commit ce127e1

16 files changed

+445
-27
lines changed

Command/Calculator.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Command.RealWorld
8+
{
9+
/// <summary>
10+
/// The 'Receiver' class
11+
/// </summary>
12+
class Calculator
13+
{
14+
private int _curr = 0;
15+
16+
public void Operation(char @operator, int operand)
17+
{
18+
switch (@operator)
19+
{
20+
case '+': _curr += operand; break;
21+
case '-': _curr -= operand; break;
22+
case '*': _curr *= operand; break;
23+
case '/': _curr /= operand; break;
24+
}
25+
Console.WriteLine(
26+
"Current value = {0,3} (following {1} {2})",
27+
_curr, @operator, operand);
28+
}
29+
}
30+
}

Command/Command.csproj

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ProjectGuid>{6DA0F093-5190-4814-8729-C9FEABCB4DDF}</ProjectGuid>
88
<OutputType>Exe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>Command</RootNamespace>
10+
<RootNamespace>DoFactory.GangOfFour.Command.RealWorld</RootNamespace>
1111
<AssemblyName>Command</AssemblyName>
1212
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
@@ -42,12 +42,19 @@
4242
<Reference Include="System.Xml" />
4343
</ItemGroup>
4444
<ItemGroup>
45-
<Compile Include="Program.cs" />
45+
<Compile Include="Calculator.cs" />
46+
<Compile Include="Command\CalculatorCommand.cs" />
47+
<Compile Include="Command\Command.cs" />
48+
<Compile Include="Program.RealWordCode.cs" />
4649
<Compile Include="Properties\AssemblyInfo.cs" />
50+
<Compile Include="User.cs" />
4751
</ItemGroup>
4852
<ItemGroup>
4953
<None Include="App.config" />
5054
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="command.gif" />
57+
</ItemGroup>
5158
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5259
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5360
Other similar extension points exist, see Microsoft.Common.targets.

Command/Command/CalculatorCommand.cs

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Command.RealWorld
8+
{
9+
10+
11+
/// <summary>
12+
/// The 'ConcreteCommand' class
13+
/// </summary>
14+
class CalculatorCommand : Command
15+
{
16+
private char _operator;
17+
private int _operand;
18+
private Calculator _calculator;
19+
20+
// Constructor
21+
public CalculatorCommand(Calculator calculator,
22+
char @operator, int operand)
23+
{
24+
this._calculator = calculator;
25+
this._operator = @operator;
26+
this._operand = operand;
27+
}
28+
29+
// Gets operator
30+
public char Operator
31+
{
32+
set { _operator = value; }
33+
}
34+
35+
// Get operand
36+
public int Operand
37+
{
38+
set { _operand = value; }
39+
}
40+
41+
// Execute new command
42+
public override void Execute()
43+
{
44+
_calculator.Operation(_operator, _operand);
45+
}
46+
47+
// Unexecute last command
48+
public override void UnExecute()
49+
{
50+
_calculator.Operation(Undo(_operator), _operand);
51+
}
52+
53+
// Returns opposite operator for given operator
54+
private char Undo(char @operator)
55+
{
56+
switch (@operator)
57+
{
58+
case '+': return '-';
59+
case '-': return '+';
60+
case '*': return '/';
61+
case '/': return '*';
62+
default:
63+
throw new
64+
ArgumentException("@operator");
65+
}
66+
}
67+
}
68+
}

Command/Command/Command.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Command.RealWorld
8+
{
9+
10+
/// <summary>
11+
/// The 'Command' abstract class
12+
/// </summary>
13+
abstract class Command
14+
{
15+
public abstract void Execute();
16+
public abstract void UnExecute();
17+
}
18+
}

Command/Program.RealWordCode.cs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+

2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace DoFactory.GangOfFour.Command.RealWorld
6+
{
7+
/// <summary>
8+
/// MainApp startup class for Real-World
9+
/// Command Design Pattern.
10+
/// </summary>
11+
class MainApp
12+
{
13+
/// <summary>
14+
/// Entry point into console application.
15+
/// </summary>
16+
static void Main()
17+
{
18+
// Create user and let her compute
19+
User user = new User();
20+
21+
// User presses calculator buttons
22+
user.Compute('+', 100);
23+
user.Compute('-', 50);
24+
user.Compute('*', 10);
25+
user.Compute('/', 2);
26+
27+
// Undo 4 commands
28+
user.Undo(4);
29+
30+
// Redo 3 commands
31+
user.Redo(3);
32+
33+
// Wait for user
34+
Console.ReadKey();
35+
}
36+
}
37+
}
38+
/*
39+
OUTPUT:
40+
Current value = 100(following + 100)
41+
Current value = 50(following - 50)
42+
Current value = 500(following * 10)
43+
Current value = 250(following / 2)
44+
45+
---- Undo 4 levels
46+
Current value = 500 (following* 2)
47+
Current value = 50(following / 10)
48+
Current value = 100(following + 50)
49+
Current value = 0(following - 100)
50+
51+
---- Redo 3 levels
52+
Current value = 100 (following + 100)
53+
Current value = 50(following - 50)
54+
Current value = 500(following * 10)
55+
*/

Command/Program.cs

-15
This file was deleted.

Command/User.cs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Command.RealWorld
8+
{
9+
/// <summary>
10+
/// The 'Invoker' class
11+
/// </summary>
12+
class User
13+
{
14+
// Initializers
15+
private Calculator _calculator = new Calculator();
16+
private List<Command> _commands = new List<Command>();
17+
private int _current = 0;
18+
19+
public void Redo(int levels)
20+
{
21+
Console.WriteLine("\n---- Redo {0} levels ", levels);
22+
// Perform redo operations
23+
for (int i = 0; i < levels; i++)
24+
{
25+
if (_current < _commands.Count - 1)
26+
{
27+
Command command = _commands[_current++];
28+
command.Execute();
29+
}
30+
}
31+
}
32+
33+
public void Undo(int levels)
34+
{
35+
Console.WriteLine("\n---- Undo {0} levels ", levels);
36+
// Perform undo operations
37+
for (int i = 0; i < levels; i++)
38+
{
39+
if (_current > 0)
40+
{
41+
Command command = _commands[--_current] as Command;
42+
command.UnExecute();
43+
}
44+
}
45+
}
46+
47+
public void Compute(char @operator, int operand)
48+
{
49+
// Create command operation and execute it
50+
Command command = new CalculatorCommand(
51+
_calculator, @operator, operand);
52+
command.Execute();
53+
54+
// Add command to undo list
55+
_commands.Add(command);
56+
_current++;
57+
}
58+
}
59+
}

Command/command.gif

5.56 KB
Loading

Decorator/Book.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Decorator.RealWorld
8+
{
9+
/// <summary>
10+
/// The 'ConcreteComponent' class
11+
/// </summary>
12+
public class Book : LibraryItem
13+
{
14+
private string _author;
15+
private string _title;
16+
17+
// Constructor
18+
public Book(string author, string title, int numCopies)
19+
{
20+
this._author = author;
21+
this._title = title;
22+
this.NumCopies = numCopies;
23+
}
24+
25+
public override void Display()
26+
{
27+
Console.WriteLine("\nBook ------ ");
28+
Console.WriteLine(" Author: {0}", _author);
29+
Console.WriteLine(" Title: {0}", _title);
30+
Console.WriteLine(" # Copies: {0}", NumCopies);
31+
}
32+
}
33+
}

Decorator/Borrowable.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Decorator.RealWorld
8+
{
9+
/// <summary>
10+
/// The 'ConcreteDecorator' class
11+
/// </summary>
12+
public class Borrowable : Decorator
13+
{
14+
protected List<string> borrowers = new List<string>();
15+
16+
// Constructor
17+
public Borrowable(LibraryItem libraryItem)
18+
: base(libraryItem)
19+
{
20+
21+
}
22+
23+
public void BorrowItem(string name)
24+
{
25+
borrowers.Add(name);
26+
libraryItem.NumCopies--;
27+
}
28+
29+
public void ReturnItem(string name)
30+
{
31+
borrowers.Remove(name);
32+
libraryItem.NumCopies++;
33+
}
34+
35+
public override void Display()
36+
{
37+
base.Display();
38+
foreach (string borrower in borrowers)
39+
{
40+
Console.WriteLine(" borrower: " + borrower);
41+
}
42+
}
43+
}
44+
}

Decorator/Decorator.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace DoFactory.GangOfFour.Decorator.RealWorld
8+
{
9+
/// <summary>
10+
/// The 'Decorator' abstract class
11+
/// </summary>
12+
public abstract class Decorator : LibraryItem
13+
{
14+
protected LibraryItem libraryItem;
15+
16+
// Constructor
17+
public Decorator(LibraryItem libraryItem)
18+
{
19+
this.libraryItem = libraryItem;
20+
}
21+
22+
public override void Display()
23+
{
24+
libraryItem.Display();
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)