摘自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);
}
//-------------------------------------------------------------------------