2. MyEventObject 类
1:
2: //MyEventObject.cpp
3: #include "MyEventObject.h"
4: #include
5:
6: //
7: void MyEventObject::Print(std::ostream& os) const
8: {
9: MyIndent indent;
10:
11: this->PrintHeader(os,0);
12: this->PrintSelf(os, indent.GetNextIndent());
13: this->PrintTrailer(os,0);
14: }
15:
16: //默认实现
17: void MyEventObject::PrintHeader(std::ostream& os, MyIndent indent) const
18: {
19: os < 20: os <"itk::" <<this->GetEventName() <<" (" <<this <<")/n"; 21: }
22:
23: //
24: void MyEventObject::PrintTrailer(std::ostream& os, MyIndent indent) const
25: {
26: os < 27: }
28:
29: void MyEventObject::PrintSelf(std::ostream&, MyIndent) const
30: {
31: }
1: //MyIndent.cpp
2: #pragma once
3: #include "MyIndent.h"
4:
5: #define ITK_STD_INDENT 4 //标准缩进量
6: #define ITK_NUMBER_OF_BLANKS 40 //最大缩进量
7:
8: //空格
9: static const char blanks[ITK_NUMBER_OF_BLANKS&#43;1] &#61; " ";
10:
11: MyIndent* MyIndent::New()
12: {
13: return new Self;
14: }
15:
16:
17: //设置下一个缩排级别
18: MyIndent MyIndent::GetNextIndent()
19: {
20: int indent &#61; m_Indent &#43; ITK_STD_INDENT;
21: if ( indent > ITK_NUMBER_OF_BLANKS )
22: {
23: indent &#61; ITK_NUMBER_OF_BLANKS;
24: }
25: return indent;
26: }
27:
28: //以锯齿状打印缩排格式&#xff0c;用空格控制
29: //该函数的实现不能放在 头文件中, 否则在多个文件中包含会造成重复定义的错误
30: std::ostream& operator<<(std::ostream& os, const MyIndent& ind)
31: {
32: //输出 ind.m_Indent 个空格...
33: os < 34: return os;
35: }
1:
2: #include "MyObject.h"
3: #include "MyCommand.h"
4: #include "SubjectObserver.h"
5: #include
6:
7: //Object 类成员函数实现:
8: MyObject::MyObject() : m_Subject(NULL)
9: {
10: //this->Modified();
11: }
12: MyObject::~MyObject()
13: {
14: delete m_Subject;
15: }
16:
17: //New() 创建对象实例
18: MyObject* MyObject::New()
19: {
20: return new Self;
21: }
22: //Delete() 销毁对象
23: void MyObject::Delete()
24: {
25: delete this;
26: }
27: //返回指定 Command 指针
28: MyCommand* MyObject::GetCommand(unsigned long tag)
29: {
30: if (this->m_Subject)
31: {
32: return this->m_Subject->GetCommand(tag);
33: }
34: return NULL;
35: }
36:
37: //注册观察者以及其感兴趣的事件
38: //程序中的 optimizer->AddObserver(MyIterationEvent(), observer); 即为此函数;
39: //MyOptimizer 继承自 MyObject,而其 AddObserver 又调用了 Subject 中的 AddObserver
40: unsigned long MyObject::AddObserver(const MyEventObject & event, MyCommand *cmd)
41: {
42: if (!this->m_Subject)
43: {
44: this->m_Subject &#61; new Subject;
45: }
46: //optimizer 中的 AddObserver 调用了 Subject 中的 AddObserver
47: return this->m_Subject->AddObserver(event, cmd);
48: }
49: //
50: unsigned long MyObject::AddObserver(const MyEventObject & event, MyCommand *cmd) const
51: {
52: if (!this->m_Subject)
53: {
54: //去掉 MyObject 的 const 属性
55: Self * me &#61; const_cast( this );
56: me->m_Subject &#61; new Subject;
57: }
58: return this->m_Subject->AddObserver(event,cmd);
59: }
60: //
61: void MyObject::RemoveObserver(unsigned long tag)
62: {
63: if (this->m_Subject)
64: {
65: this->m_Subject->RemoveObserver(tag);
66: }
67: }
68: //
69: void MyObject::RemoveAllObservers()
70: {
71: if (this->m_Subject)
72: {
73: this->m_Subject->RemoveAllObservers();
74: }
75: }
76:
77: //InvokeEvent(): 触发特定事件的发生
78: //优化程序每迭代一次, 便调用该方法触发一次 MyIerationEvent() 事件
79: //而 InvokeEvent() 调用了 subject 中的 InvokeEvent()
80: void MyObject::InvokeEvent( const MyEventObject & event )
81: {
82: if (this->m_Subject)
83: {
84: this->m_Subject->InvokeEvent(event,this);
85: }
86: }
87: //
88: void MyObject::InvokeEvent( const MyEventObject & event ) const
89: {
90: if (this->m_Subject)
91: {
92: this->m_Subject->InvokeEvent(event,this);
93: }
94: }
95: //检查是观察者注册了 event 事件
96: bool MyObject::HasObserver( const MyEventObject & event ) const
97: {
98: if (this->m_Subject)
99: {
100: return this->m_Subject->HasObserver(event);
101: }
102: return false;
103: }
104: //打印观察者相关信息
105: bool MyObject::PrintObservers(std::ostream& os, MyIndent indent) const
106: {
107: if (this->m_Subject)
108: {
109: return this->m_Subject->PrintObservers(os, indent);
110: }
111: return false;
112: }
113:
114: //ITK 中, Print() 对所有对象都是一样的, 它只是调用了相应的
115: //header/self/trailer 虚方法
116: void MyObject::Print(std::ostream& os, MyIndent indent) const
117: {
118: this->PrintHeader(os, indent);
119: this->PrintSelf(os, indent.GetNextIndent());
120: this->PrintTrailer(os, indent);
121: }
122:
123: //默认实现
124: void MyObject::PrintHeader(std::ostream& os, MyIndent indent) const
125: {
126: os <"RTTI typeinfo: " 127: <this ).name() < 128: }
129: //打印自身的相关信息
130: void MyObject::PrintSelf(std::ostream& os, MyIndent indent) const
131: {
132: os <"Observers: " < 133:
134: if(!this->PrintObservers(os, indent.GetNextIndent()))
135: {
136: os <"none/n"; 137: }
138: }
139: //默认实现
140: void MyObject::PrintTrailer(std::ostream& os, MyIndent indent) const
141: {
142: }
143:
144: //该方法允许 MyObject 的所有子类通过 <<输出自己的相关信息, 它调用了 Print() 成员方法
145: std::ostream& operator<<(std::ostream& os, const MyObject& o)
146: {
147: o.Print(os);
148: return os;
149: }