sigslot简介

sigslot简介

2009年02月24日  |本网站遵守CC版权协议 转载请注明出自www.thecodeway.com

    在开发一个复杂工程的时候,经常会遇到这样一个问题:整个系统被分成数个模块,每个模块提供有限的功能,由上层调用组成整个系统,为了保证每个模块的独立性,我们经常会尽量限制模块与模块之间的直接联系,比如每个模块只提供有限的API或者COM接口,而内部实现则完全封闭起来。
    但有的时候会出一些设计要求,必须能够使模块之间能够直接通讯,而这两个模块往往处于不同的逻辑层次,之间相差甚远,如何设计它们之间的调用模式使整个工程维持整洁变得非常困难,比如模块直接直接包含对方的头文件会引起编译变得复杂,提供api或者接口会引起版本危机等问题。
    sigslot的出现为我们提供了一种解决问题的思想,它用“信号”的概念实现不同模块之间的传输问题,sigslot本身类似于一条通讯电缆,两端提供发送器和接收器,只要把两个模块用这条电缆连接起来就可以实现接口调用,而sigslot本身只是一个轻量级的作品,整个库只有一个.h文件,所以无论处于何种层次的库,都可以非常方便的包含它。

    举个例子,我们设计一个发送消息的类,这个类负责在某种时刻向外界发出求救信号

// Class that sends the notification.
class   Sender   
{
public :
    
// The signal declaration. 
    
// The ’2′ in the name indicates the number of parameters. Parameter types 
    
// are declared in the template parameter list.
    
sigslot :: signal2 std :: string  ,  int  >  SignalDanger ;
    
    
// When anyone calls Panic(), we will send the SignalDanger signal.
    
void   Panic ()
    
{
        
SignalDanger ( " Help! " 0 )
    
}
} ;

另外一个类则负责接收求助信号

// Listening class. It must inherit sigslot.
class   Receiver  :  public   sigslot :: has_slots <>
{
public :
    
// When anyone calls Panic(), Receiver::OnDanger gets the message.
    
// Notice that the number and type of parameters match
    
// those in Sender::SignalDanger, and that it doesn’t return a value.
    
void   OnDanger ( std :: string   message int   time )
    
{
        
printf ( " I heard something like  \ " % s \ "  at %d! \ n " message . c_str () time ) ;
    
}
} ;

现在让我们在主逻辑中把这两个类连接起来

Sender   sender ;
Receiver   receiver ;
 
// Receiver registers to get SignalDanger signals.
// When SignalDanger is sent, it is caught by OnDanger().
// Second parameter gives address of the listener function class definition.
// First parameter points to instance of this class to receive notifications.
sender . SignalDanger . connect ( & receiver Receiver :: OnDanger ) ;

只要在任何时候调用 sender.Panic()函数,就会把求救信号发送给接收者,而且这两个发送和接收端的模块都可以独立编译,不会出现版本问题。

你可能感兴趣的:(设计模式)