forked from QianMo/Unity-Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverStructure.cs
More file actions
132 lines (112 loc) · 3.52 KB
/
Copy pathObserverStructure.cs
File metadata and controls
132 lines (112 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//-------------------------------------------------------------------------------------
// ObserverStructure.cs
//-------------------------------------------------------------------------------------
//[Definition]
//--------------------------------
// Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
//
// [Participants]
//--------------------------------
// The classes and objects participating in this pattern are:
//
// Subject
// knows its observers.Any number of Observer objects may observe a subject
// provides an interface for attaching and detaching Observer objects.
// ConcreteSubject
// stores state of interest to ConcreteObserver
// sends a notification to its observers when its state changes
// Observer
// defines an updating interface for objects that should be notified of changes in a subject.
// ConcreteObserver
// maintains a reference to a ConcreteSubject object
// stores state that should stay consistent with the subject's
// implements the Observer updating interface to keep its state consistent with the subject's
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObserverStructure : MonoBehaviour
{
void Start()
{
// Configure Observer pattern
ConcreteSubject s = new ConcreteSubject();
s.Attach(new ConcreteObserver(s, "X"));
s.Attach(new ConcreteObserver(s, "Y"));
s.Attach(new ConcreteObserver(s, "Z"));
// Change subject and notify observers
s.SubjectState = "ABC";
s.Notify();
// Change subject and notify observers again
s.SubjectState = "666";
s.Notify();
}
}
/// <summary>
/// The 'Subject' abstract class
/// </summary>
abstract class Subject
{
private List<Observer> _observers = new List<Observer>();
public void Attach(Observer observer)
{
_observers.Add(observer);
}
public void Detach(Observer observer)
{
_observers.Remove(observer);
}
public void Notify()
{
foreach (Observer o in _observers)
{
o.Update();
}
}
}
/// <summary>
/// The 'ConcreteSubject' class
/// </summary>
class ConcreteSubject : Subject
{
private string _subjectState;
// Gets or sets subject state
public string SubjectState
{
get { return _subjectState; }
set { _subjectState = value; }
}
}
/// <summary>
/// The 'Observer' abstract class
/// </summary>
abstract class Observer
{
public abstract void Update();
}
/// <summary>
/// The 'ConcreteObserver' class
/// </summary>
class ConcreteObserver : Observer
{
private string _name;
private string _observerState;
private ConcreteSubject _subject;
// Constructor
public ConcreteObserver(
ConcreteSubject subject, string name)
{
this._subject = subject;
this._name = name;
}
public override void Update()
{
_observerState = _subject.SubjectState;
Debug.Log("Observer "+ _name+"'s new state is "+_observerState);
}
// Gets or sets subject
public ConcreteSubject Subject
{
get { return _subject; }
set { _subject = value; }
}
}