摘自BCB的例子,越看越有点迷糊,还越觉得niubility.像观察者又肯定不是,实在是牛...Main.cpp:
//----------------------------------------------------------------------------
//Borland C++Builder
//Copyright (c) 1987, 1998-2002 Borland International Inc. All Rights Reserved.
//----------------------------------------------------------------------------
//-------------------------------------------------------------------------
// minicomp.cpp - uses the TCounter example component
//-------------------------------------------------------------------------
#include "minicomp.h"
#include <stdio.h>
#include <stdlib.h>
#include <condefs.h>
//-------------------------------------------------------------------------
USEUNIT("counter.cpp");
//---------------------------------------------------------------------------
main()
{
TExample example;
return 0;
}
//-------------------------------------------------------------------------
TExample::TExample()
{
TCounter Counter(7);
int i;
//把控件Counter的接口FMultiple和外部实现MultipleReached连接上
Counter.OnMultiple = MultipleReached;
for (i=0; i < 100; i++)
Counter.Increment();
}
//-------------------------------------------------------------------------
void TExample::MultipleReached(TCounter *Sender)
{
printf("Multiple=%d reached with val=%d\n", Sender->Multiple, Sender->Val);
}
//-------------------------------------------------------------------------
minicomp.h:
//-------------------------------------------------------------------------
// minicomp.h - uses the TCounter example component
//-------------------------------------------------------------------------
#include "counter.h"
//-------------------------------------------------------------------------
class TExample
{
private:
void MultipleReached(TCounter *Sender);
public:
TExample();
};
//-------------------------------------------------------------------------
counter.h:
//-------------------------------------------------------------------------
// counter.h. - example of a small, non-visual counter component
//-------------------------------------------------------------------------
class TCounter; // forward
typedef void (__closure *TCounterEvent)(TCounter *Sender);
//-------------------------------------------------------------------------
class TCounter
{
private:
TCounterEvent FOnMultiple; //这就是个函数接口
int FVal;
int FMultiple;
public:
__property int Val = {read=FVal, write=FVal};
__property int Multiple = {read=FMultiple};
__property TCounterEvent OnMultiple = {read=FOnMultiple, write=FOnMultiple};
void Clear();
void Increment();
TCounter(int Multiple);
};
//-------------------------------------------------------------------------
counter.cpp:
//-------------------------------------------------------------------------
// counter.cpp - example of a small, non-visual counter component
//-------------------------------------------------------------------------
#include "counter.h"
//-------------------------------------------------------------------------
TCounter::TCounter(int Multiple)
{
FMultiple = Multiple;
}
//-------------------------------------------------------------------------
void TCounter::Clear()
{
FVal = 0;
}
//-------------------------------------------------------------------------
void TCounter::Increment()
{
//这句执行时都是外部来调用的,此时OnMultiple已经和外面那个函数接口连接上了
//也就是调用的其实是外面进来的那个函数,把this传出去,让外部那个函数操作
//TExample::MultipleReached(this)
if (((++FVal) % FMultiple) == 0)
OnMultiple(this);
}
//-------------------------------------------------------------------------