Observer设计模式
类型:行为型
意义: 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用:当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
D的Phobos库std.signals实现了这个模式.
也可利用委托来简单实现.
DFL的事件委托分派机制就是个Observer模式实现.
[code]import std.signals;
import std.stdio;
class Observer
{ // our slot
void watch(char[] msg, int i)
{
writefln("Observed msg '%s' and value %s", msg, i);
}
void exc(char[] msg, int i)
{
writefln("EXEC OBJECT '%s' and value %s", msg, i);
}
}
class Observera
{ // our slot
void watch(char[] msg, int i)
{
writefln("LLPF msg '%s' and value %s", msg, i);
}
}
class Foo
{
int value() { return _value; }
int value(int v)
{
if (v != _value)
{ _value = v;
// call all the connected slots with the two parameters
emit("set Value to:", v);
}
return v;
}
// Mix in all the code we need to make Foo into a signal
mixin Signal!(char[], int);
private :
int _value;
}
void main()
{
Foo a = new Foo;
Observer o = new Observer;
Observera b = new Observera;
a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.connect(&o.exc);
a.connect(&b.watch); // o.watch is the slot
a.value = 4; // should call o.watch()
a.disconnect(&o.watch); // o.watch is no longer a slot
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.value = 6; // should call o.watch()
delete o; // destroying o should automatically disconnect it
a.value = 7; // should not call o.watch()
}
[/code]
类型:行为型
意义: 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。
适用:当一个抽象模型有两个方面, 其中一个方面依赖于另一方面。将这二者封装在独立的对象中以使它们可以各自独立地改变和复用。
当对一个对象的改变需要同时改变其它对象, 而不知道具体有多少对象有待改变。
当一个对象必须通知其它对象,而它又不能假定其它对象是谁。换言之, 你不希望这些对象是紧密耦合的。
D的Phobos库std.signals实现了这个模式.
也可利用委托来简单实现.
DFL的事件委托分派机制就是个Observer模式实现.
[code]import std.signals;
import std.stdio;
class Observer
{ // our slot
void watch(char[] msg, int i)
{
writefln("Observed msg '%s' and value %s", msg, i);
}
void exc(char[] msg, int i)
{
writefln("EXEC OBJECT '%s' and value %s", msg, i);
}
}
class Observera
{ // our slot
void watch(char[] msg, int i)
{
writefln("LLPF msg '%s' and value %s", msg, i);
}
}
class Foo
{
int value() { return _value; }
int value(int v)
{
if (v != _value)
{ _value = v;
// call all the connected slots with the two parameters
emit("set Value to:", v);
}
return v;
}
// Mix in all the code we need to make Foo into a signal
mixin Signal!(char[], int);
private :
int _value;
}
void main()
{
Foo a = new Foo;
Observer o = new Observer;
Observera b = new Observera;
a.value = 3; // should not call o.watch()
a.connect(&o.watch); // o.watch is the slot
a.connect(&o.exc);
a.connect(&b.watch); // o.watch is the slot
a.value = 4; // should call o.watch()
a.disconnect(&o.watch); // o.watch is no longer a slot
a.value = 5; // so should not call o.watch()
a.connect(&o.watch); // connect again
a.value = 6; // should call o.watch()
delete o; // destroying o should automatically disconnect it
a.value = 7; // should not call o.watch()
}
[/code]