Skip to content

Commit 7132f73

Browse files
committed
add Behavioral Patterns exmaples part 1
1 parent 5d16638 commit 7132f73

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+781
-18
lines changed

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example1.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//-------------------------------------------------------------------------------------
2+
// ChainOfResponsibilityExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
//This real-world code demonstrates the Chain of Responsibility pattern in which several linked
6+
//managers and executives can respond to a purchase request or hand it off to a superior.
7+
//Each position has can have its own set of rules which orders they can approve.
8+
9+
10+
using UnityEngine;
11+
using System.Collections;
12+
13+
namespace ChainOfResponsibilityExample1
14+
{
15+
public class ChainOfResponsibilityExample1 : MonoBehaviour
16+
{
17+
void Start ( )
18+
{
19+
// Setup Chain of Responsibility
20+
Approver larry = new Director();
21+
Approver sam = new VicePresident();
22+
Approver tammy = new President();
23+
24+
larry.SetSuccessor(sam);
25+
sam.SetSuccessor(tammy);
26+
27+
// Generate and process purchase requests
28+
Purchase p = new Purchase(2034, 350.00, "Assets");
29+
larry.ProcessRequest(p);
30+
31+
p = new Purchase(2035, 32590.10, "Project X");
32+
larry.ProcessRequest(p);
33+
34+
p = new Purchase(2036, 122100.00, "Project Y");
35+
larry.ProcessRequest(p);
36+
37+
}
38+
}
39+
40+
/// <summary>
41+
/// The 'Handler' abstract class
42+
/// </summary>
43+
abstract class Approver
44+
{
45+
protected Approver successor;
46+
47+
public void SetSuccessor(Approver successor)
48+
{
49+
this.successor = successor;
50+
}
51+
52+
public abstract void ProcessRequest(Purchase purchase);
53+
}
54+
55+
/// <summary>
56+
/// The 'ConcreteHandler' class
57+
/// </summary>
58+
class Director : Approver
59+
{
60+
public override void ProcessRequest(Purchase purchase)
61+
{
62+
if (purchase.Amount < 10000.0)
63+
{
64+
Debug.Log(this.GetType().Name+" approved request# "+purchase.Number);
65+
}
66+
else if (successor != null)
67+
{
68+
successor.ProcessRequest(purchase);
69+
}
70+
}
71+
}
72+
73+
/// <summary>
74+
/// The 'ConcreteHandler' class
75+
/// </summary>
76+
class VicePresident : Approver
77+
{
78+
public override void ProcessRequest(Purchase purchase)
79+
{
80+
if (purchase.Amount < 25000.0)
81+
{
82+
Debug.Log(this.GetType().Name + " approved request# " + purchase.Number);
83+
}
84+
else if (successor != null)
85+
{
86+
successor.ProcessRequest(purchase);
87+
}
88+
}
89+
}
90+
91+
/// <summary>
92+
/// The 'ConcreteHandler' class
93+
/// </summary>
94+
class President : Approver
95+
{
96+
public override void ProcessRequest(Purchase purchase)
97+
{
98+
if (purchase.Amount < 100000.0)
99+
{
100+
Debug.Log(this.GetType().Name + " approved request# " + purchase.Number);
101+
}
102+
else
103+
{
104+
Debug.Log("Request# "+purchase.Number+ "requires an executive meeting!");
105+
}
106+
}
107+
}
108+
109+
/// <summary>
110+
/// Class holding request details
111+
/// </summary>
112+
class Purchase
113+
{
114+
private int _number;
115+
private double _amount;
116+
private string _purpose;
117+
118+
// Constructor
119+
public Purchase(int number, double amount, string purpose)
120+
{
121+
this._number = number;
122+
this._amount = amount;
123+
this._purpose = purpose;
124+
}
125+
126+
// Gets or sets purchase number
127+
public int Number
128+
{
129+
get { return _number; }
130+
set { _number = value; }
131+
}
132+
133+
// Gets or sets purchase amount
134+
public double Amount
135+
{
136+
get { return _amount; }
137+
set { _amount = value; }
138+
}
139+
140+
// Gets or sets purchase purpose
141+
public string Purpose
142+
{
143+
get { return _purpose; }
144+
set { _purpose = value; }
145+
}
146+
}
147+
}

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example1/ChainOfResponsibilityExample1.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Behavioral Patterns/Chain of Responsibility Pattern/Example1/TestChainOfRespExample1.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Behavioral Patterns/Command Pattern/Example1.meta

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//-------------------------------------------------------------------------------------
2+
// CommandExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
//This real-world code demonstrates the Command pattern used in a simple calculator with unlimited number of undo's and redo's.
6+
//Note that in C# the word 'operator' is a keyword. Prefixing it with '@' allows using it as an identifier.
7+
8+
using System;
9+
using UnityEngine;
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
13+
namespace CommandExample1
14+
{
15+
public class CommandExample1 : MonoBehaviour
16+
{
17+
void Start ( )
18+
{
19+
// Create user and let her compute
20+
User user = new User();
21+
22+
// User presses calculator buttons
23+
user.Compute('+', 100);
24+
user.Compute('-', 50);
25+
user.Compute('*', 10);
26+
user.Compute('/', 2);
27+
28+
// Undo 4 commands
29+
user.Undo(4);
30+
31+
// Redo 3 commands
32+
user.Redo(3);
33+
}
34+
}
35+
36+
/// <summary>
37+
/// The 'Command' abstract class
38+
/// </summary>
39+
abstract class Command
40+
{
41+
public abstract void Execute();
42+
public abstract void UnExecute();
43+
}
44+
45+
/// <summary>
46+
/// The 'ConcreteCommand' class
47+
/// </summary>
48+
class CalculatorCommand : Command
49+
{
50+
private char _operator;
51+
private int _operand;
52+
private Calculator _calculator;
53+
54+
// Constructor
55+
public CalculatorCommand(Calculator calculator,
56+
char @operator, int operand)
57+
{
58+
this._calculator = calculator;
59+
this._operator = @operator;
60+
this._operand = operand;
61+
}
62+
63+
// Gets operator
64+
public char Operator
65+
{
66+
set { _operator = value; }
67+
}
68+
69+
// Get operand
70+
public int Operand
71+
{
72+
set { _operand = value; }
73+
}
74+
75+
// Execute new command
76+
public override void Execute()
77+
{
78+
_calculator.Operation(_operator, _operand);
79+
}
80+
81+
// Unexecute last command
82+
public override void UnExecute()
83+
{
84+
_calculator.Operation(Undo(_operator), _operand);
85+
}
86+
87+
// Returns opposite operator for given operator
88+
private char Undo(char @operator)
89+
{
90+
switch (@operator)
91+
{
92+
case '+': return '-';
93+
case '-': return '+';
94+
case '*': return '/';
95+
case '/': return '*';
96+
default:
97+
throw new
98+
ArgumentException("@operator");
99+
}
100+
}
101+
}
102+
103+
/// <summary>
104+
/// The 'Receiver' class
105+
/// </summary>
106+
class Calculator
107+
{
108+
private int _curr = 0;
109+
110+
public void Operation(char @operator, int operand)
111+
{
112+
switch (@operator)
113+
{
114+
case '+': _curr += operand; break;
115+
case '-': _curr -= operand; break;
116+
case '*': _curr *= operand; break;
117+
case '/': _curr /= operand; break;
118+
}
119+
Debug.Log("Current value = " + _curr+ " ( following "+ @operator+operand+" )");
120+
}
121+
}
122+
123+
/// <summary>
124+
/// The 'Invoker' class
125+
/// </summary>
126+
class User
127+
{
128+
// Initializers
129+
private Calculator _calculator = new Calculator();
130+
private List<Command> _commands = new List<Command>();
131+
private int _current = 0;
132+
133+
public void Redo(int levels)
134+
{
135+
for (int i = 0; i < levels; i++)
136+
{
137+
if (_current < _commands.Count - 1)
138+
{
139+
Command command = _commands[_current++];
140+
command.Execute();
141+
}
142+
}
143+
}
144+
145+
public void Undo(int levels)
146+
{
147+
Debug.Log("\n---- Undo "+ levels + " levels");
148+
// Perform undo operations
149+
for (int i = 0; i < levels; i++)
150+
{
151+
if (_current > 0)
152+
{
153+
Command command = _commands[--_current] as Command;
154+
command.UnExecute();
155+
}
156+
}
157+
}
158+
159+
public void Compute(char @operator, int operand)
160+
{
161+
// Create command operation and execute it
162+
Command command = new CalculatorCommand(
163+
_calculator, @operator, operand);
164+
command.Execute();
165+
166+
// Add command to undo list
167+
_commands.Add(command);
168+
_current++;
169+
}
170+
}
171+
}

Assets/Behavioral Patterns/Command Pattern/Example1/CommandExample1.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Behavioral Patterns/Command Pattern/Example1/TestCommandExample1.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)