Skip to content

Commit 179337d

Browse files
committed
add Behavioral Patterns examples Part 2
1 parent 59bce89 commit 179337d

16 files changed

+531
-0
lines changed

Assets/Behavioral Patterns/Mediator 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: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
//-------------------------------------------------------------------------------------
2+
// MediatorExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
//This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between
10+
//different Participants registering with a Chatroom.The Chatroom is the central hub through which all communication takes place.
11+
//At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.
12+
13+
namespace MediatorExample1
14+
{
15+
public class MediatorExample1 : MonoBehaviour
16+
{
17+
void Start()
18+
{
19+
// Create chatroom
20+
Chatroom chatroom = new Chatroom();
21+
22+
// Create participants and register them
23+
Participant George = new Beatle("George");
24+
Participant Paul = new Beatle("Paul");
25+
Participant Ringo = new Beatle("Ringo");
26+
Participant John = new Beatle("John");
27+
Participant Yoko = new NonBeatle("Yoko");
28+
29+
chatroom.Register(George);
30+
chatroom.Register(Paul);
31+
chatroom.Register(Ringo);
32+
chatroom.Register(John);
33+
chatroom.Register(Yoko);
34+
35+
// Chatting participants
36+
Yoko.Send("John", "Hi John!");
37+
Paul.Send("Ringo", "All you need is love");
38+
Ringo.Send("George", "My sweet Lord");
39+
Paul.Send("John", "Can't buy me love");
40+
John.Send("Yoko", "My sweet love");
41+
42+
}
43+
}
44+
45+
/// <summary>
46+
/// The 'Mediator' abstract class
47+
/// </summary>
48+
abstract class AbstractChatroom
49+
{
50+
public abstract void Register(Participant participant);
51+
public abstract void Send(string from, string to, string message);
52+
}
53+
54+
/// <summary>
55+
/// The 'ConcreteMediator' class
56+
/// </summary>
57+
class Chatroom : AbstractChatroom
58+
{
59+
private Dictionary<string, Participant> _participants =
60+
new Dictionary<string, Participant>();
61+
62+
public override void Register(Participant participant)
63+
{
64+
if (!_participants.ContainsValue(participant))
65+
{
66+
_participants[participant.Name] = participant;
67+
}
68+
69+
participant.Chatroom = this;
70+
}
71+
72+
public override void Send( string from, string to, string message)
73+
{
74+
Participant participant = _participants[to];
75+
76+
if (participant != null)
77+
{
78+
participant.Receive(from, message);
79+
}
80+
}
81+
}
82+
83+
/// <summary>
84+
/// The 'AbstractColleague' class
85+
/// </summary>
86+
class Participant
87+
{
88+
private Chatroom _chatroom;
89+
private string _name;
90+
91+
// Constructor
92+
public Participant(string name)
93+
{
94+
this._name = name;
95+
}
96+
97+
// Gets participant name
98+
public string Name
99+
{
100+
get { return _name; }
101+
}
102+
103+
// Gets chatroom
104+
public Chatroom Chatroom
105+
{
106+
set { _chatroom = value; }
107+
get { return _chatroom; }
108+
}
109+
110+
// Sends message to given participant
111+
public void Send(string to, string message)
112+
{
113+
_chatroom.Send(_name, to, message);
114+
}
115+
116+
// Receives message from given participant
117+
public virtual void Receive(string from, string message)
118+
{
119+
Debug.Log(from + " to " + Name + ": '" + message + "'");
120+
}
121+
}
122+
123+
/// <summary>
124+
/// A 'ConcreteColleague' class
125+
/// </summary>
126+
class Beatle : Participant
127+
{
128+
// Constructor
129+
public Beatle(string name)
130+
: base(name)
131+
{
132+
}
133+
134+
public override void Receive(string from, string message)
135+
{
136+
Debug.Log("To a Beatle: ");
137+
base.Receive(from, message);
138+
}
139+
}
140+
141+
/// <summary>
142+
/// A 'ConcreteColleague' class
143+
/// </summary>
144+
class NonBeatle : Participant
145+
{
146+
// Constructor
147+
public NonBeatle(string name)
148+
: base(name)
149+
{
150+
}
151+
152+
public override void Receive(string from, string message)
153+
{
154+
Debug.Log("To a non-Beatle: ");
155+
base.Receive(from, message);
156+
}
157+
}
158+
}

Assets/Behavioral Patterns/Mediator Pattern/Example1/MediatorExample1.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/Mediator Pattern/Example1/TestMediatorExample1.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/Memento 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: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//-------------------------------------------------------------------------------------
2+
// MediatorExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
//This real-world code demonstrates the Memento pattern which temporarily saves and then restores the SalesProspect's internal state.
9+
10+
namespace MementoExample1
11+
{
12+
public class MementoExample1 : MonoBehaviour
13+
{
14+
void Start()
15+
{
16+
SalesProspect s = new SalesProspect();
17+
s.Name = "Bruce";
18+
s.Phone = "(412) 256-6666";
19+
s.Budget = 25000.0;
20+
21+
// Store internal state
22+
ProspectMemory m = new ProspectMemory();
23+
m.Memento = s.SaveMemento();
24+
25+
// Continue changing originator
26+
s.Name = "Oliver";
27+
s.Phone = "(310) 209-8888";
28+
s.Budget = 1000000.0;
29+
30+
// Restore saved state
31+
s.RestoreMemento(m.Memento);
32+
33+
}
34+
}
35+
36+
/// <summary>
37+
/// The 'Originator' class
38+
/// </summary>
39+
class SalesProspect
40+
{
41+
private string _name;
42+
private string _phone;
43+
private double _budget;
44+
45+
// Gets or sets name
46+
public string Name
47+
{
48+
get { return _name; }
49+
set
50+
{
51+
_name = value;
52+
Debug.Log("Name: " + _name);
53+
}
54+
}
55+
56+
// Gets or sets phone
57+
public string Phone
58+
{
59+
get { return _phone; }
60+
set
61+
{
62+
_phone = value;
63+
Debug.Log("Phone: " + _phone);
64+
}
65+
}
66+
67+
// Gets or sets budget
68+
public double Budget
69+
{
70+
get { return _budget; }
71+
set
72+
{
73+
_budget = value;
74+
Debug.Log("Budget: " + _budget);
75+
}
76+
}
77+
78+
// Stores memento
79+
public Memento SaveMemento()
80+
{
81+
Debug.Log("\nSaving state --\n");
82+
return new Memento(_name, _phone, _budget);
83+
}
84+
85+
// Restores memento
86+
public void RestoreMemento(Memento memento)
87+
{
88+
Debug.Log("\nRestoring state --\n");
89+
this.Name = memento.Name;
90+
this.Phone = memento.Phone;
91+
this.Budget = memento.Budget;
92+
}
93+
}
94+
95+
/// <summary>
96+
/// The 'Memento' class
97+
/// </summary>
98+
class Memento
99+
{
100+
private string _name;
101+
private string _phone;
102+
private double _budget;
103+
104+
// Constructor
105+
public Memento(string name, string phone, double budget)
106+
{
107+
this._name = name;
108+
this._phone = phone;
109+
this._budget = budget;
110+
}
111+
112+
// Gets or sets name
113+
public string Name
114+
{
115+
get { return _name; }
116+
set { _name = value; }
117+
}
118+
119+
// Gets or set phone
120+
public string Phone
121+
{
122+
get { return _phone; }
123+
set { _phone = value; }
124+
}
125+
126+
// Gets or sets budget
127+
public double Budget
128+
{
129+
get { return _budget; }
130+
set { _budget = value; }
131+
}
132+
}
133+
134+
/// <summary>
135+
/// The 'Caretaker' class
136+
/// </summary>
137+
class ProspectMemory
138+
{
139+
private Memento _memento;
140+
141+
// Property
142+
public Memento Memento
143+
{
144+
set { _memento = value; }
145+
get { return _memento; }
146+
}
147+
}
148+
}

Assets/Behavioral Patterns/Memento Pattern/Example1/MementoExample1.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/Memento Pattern/Example1/TestMementoExample1.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)