ITK 配准框架中的 Subject/Observer 模式及优化过程模拟演示-3

      这里先给出程序的主要类图:一些成员函数我以简写的方式在类图中给出,如 Add() 表示 AddObserver() 等,类中只列出了一些比较重要的数据成员及方法。还有类之间的聚集关系表示的可能不太对,VISIO 太会操作,自己对 UML 也不是很熟悉,差不多意思是可以表示明白的。

1. 类继承结构图:

ITK 配准框架中的 Subject/Observer 模式及优化过程模拟演示-3_第1张图片

2. Command/Observer 模式的类结构图:

ITK 配准框架中的 Subject/Observer 模式及优化过程模拟演示-3_第2张图片

以上两个类结构图与 ITK 中基本相似,以后会对图像配准源代码的执行流程给出一个简单的分析,并给出用到的主要类之间的结构图。

下面给出对应头文件的实现代码:

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 << std::endl;
  20:      os << indent << "itk::" << this->GetEventName() << " (" << this << ")/n";
  21:  }
  22:   
  23:  //
  24:  void MyEventObject::PrintTrailer(std::ostream& os, MyIndent indent) const
  25:  {
  26:      os << indent << std::endl;
  27:  }
  28:   
  29:  void MyEventObject::PrintSelf(std::ostream&, MyIndent) const
  30:  {
  31:  }

3.MyIndent 类:

   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+1] = "                                        ";
  10:   
  11:  MyIndent* MyIndent::New()
  12:  {
  13:      return new Self;
  14:  }
  15:   
  16:   
  17:  //设置下一个缩排级别
  18:  MyIndent MyIndent::GetNextIndent()
  19:  {
  20:      int indent = m_Indent + ITK_STD_INDENT;
  21:      if ( indent > ITK_NUMBER_OF_BLANKS )
  22:      {
  23:          indent = ITK_NUMBER_OF_BLANKS;
  24:      }
  25:      return indent;
  26:  }
  27:   
  28:  //以锯齿状打印缩排格式,用空格控制
  29:  //该函数的实现不能放在 头文件中, 否则在多个文件中包含会造成重复定义的错误
  30:  std::ostream& operator<<(std::ostream& os, const MyIndent& ind)
  31:  {
  32:      //输出 ind.m_Indent 个空格...
  33:      os << blanks + (ITK_NUMBER_OF_BLANKS-ind.m_Indent);
  34:      return os;
  35:  }

 

4.MyObject 类:

   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 = 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 = const_cast
   
    ( 
    this );
   
  56:          me->m_Subject = 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 << indent << "RTTI typeinfo:   " 
 127:          << typeid( *this ).name() << std::endl;
 128:  }
 129:  //打印自身的相关信息
 130:  void MyObject::PrintSelf(std::ostream& os, MyIndent indent) const
 131:  {    
 132:      os << indent << "Observers: " << std::endl;
 133:      
 134:      if(!this->PrintObservers(os, indent.GetNextIndent()))
 135:      {
 136:          os << indent.GetNextIndent() <<"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:  }

你可能感兴趣的:(ITK 配准框架中的 Subject/Observer 模式及优化过程模拟演示-3)