-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathobserver pattern.scd
43 lines (31 loc) · 1.16 KB
/
observer pattern.scd
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
// Apart from that... you can have a "thing" (an object) broadcast that it has changed (`myObject.changed(\value, ...)`) and you can register other objects to receive notifications (`~responder = SimpleController(myObject).put(\value, { ... do something... })`) -- the Observer design pattern. This is exactly the kind of modularity you're talking about -- the thing that is changing doesn't have to know anything about the actions that will follow, and the receivers don't have to care how it changed.
// (One detail is to make sure you remove the responders -- if it's a SimpleController, `~responder.remove`.)
// With a class like this:
ValueNotify : Ref {
set { |thing|
value = thing;
this.changed(\value, value);
}
value_ { |thing|
value = thing;
this.changed(\value, value);
}
printOn { |stream|
stream << "ValueNotify(" << value << ")";
}
storeOn { |stream|
stream << "ValueNotify(" <<< value << ")";
}
}
// You could do:
f = ValueNotify(440);
r = SimpleController(f).put(\value, { |obj, what, value|
a.set(\freq, value);
});
a = Synth(\default);
// your OSCdef would do this:
// note there is no reference here to 'a'!
f.value = 220;
f.value = 660;
a.free;
r.remove;