Delphi的介面委託示例

2021-09-07 21:25:40 字數 3247 閱讀 4811

unit testunit;

inte***ce

uses

windows, messages, sysutils, variants, classes, graphics, controls, forms,

dialogs, stdctrls;

const

testmsgguid: tguid = '';

type

//原始介面中新增需要暴露給其它模組的介面定義,公用部分

itestmsg = inte***ce

['']

procedure showtestmsg;

end;

//---------------------------------服務模組

//基類物件,只需要開放showtestmsg方法給外部,所以做為按口的實現基類

tbasetestmsg = class(tinte***cedobject, itestmsg)

public

//.... 模組已存在的老**....

//新開放的介面**方法

procedure showtestmsg; virtual;     //申明成虛擬方法,以便繼承類可以過載

end;

//---------------------------------介面委託物件定義

ttestmsgclass = class(tinte***cedobject, itestmsg)

private

ftestmsg: itestmsg;

public

property service: itestmsg read ftestmsg implements itestmsg;

constructor create(aclass: tclass);

constructor createex(aclass: tclass);      //另一種用法, 不採用tbasetestmsg做為基類建立委託例項

destructor destroy; override;

end;

//----------------------------------外部引用的業務模組

//完成具體業務的委託例項

tetestmsg = class(tinte***cedobject, itestmsg)

public

procedure showtestmsg;

end;

//完成具體業務的委託例項

tctestmsg = class(tinte***cedobject, itestmsg)

public

procedure showtestmsg;

end;

tform1 = class(tform)

button1: tbutton;

button2: tbutton;

procedure button1click(sender: tobject);

procedure button2click(sender: tobject);

private

public

procedure dotest(aclass: tclass; acreateex: boolean = false);     //測試方法

end;

varform1: tform1;

implementation

procedure tbasetestmsg.showtestmsg;

begin

end;

constructor ttestmsgclass.create(aclass: tclass);

varvobj: tbasetestmsg;

begin

vobj := tbasetestmsg(aclass.newinstance);

ftestmsg := vobj.create;

end;

constructor ttestmsgclass.createex(aclass: tclass);

begin

//該方法不採用tbasetestmsg做為基類建立委託例項,更通用更靈活

(aclass.newinstance.create).getinte***ce(testmsgguid, ftestmsg);

end;

destructor ttestmsgclass.destroy;

begin

ftestmsg := nil;

inherited;

end;

procedure tetestmsg.showtestmsg;

begin

showmessage('tetestmsg msg:' + 'ok');

end;

procedure tctestmsg.showtestmsg;

begin

showmessage('tctestmsg 訊息:' + '好的');

end;

//--------------------以下為測試**--------------------------------

procedure tform1.dotest(aclass: tclass; acreateex: boolean);

varvclass: ttestmsgclass;

vtest: itestmsg;

begin

if acreateex then

vclass := ttestmsgclass.createex(aclass)

else

vclass := ttestmsgclass.create(aclass);

tryvtest := vclass;

vtest.showtestmsg;

finally

vtest := nil;

freeandnil(vclass);

end;

end;

procedure tform1.button1click(sender: tobject);

begin

dotest(tetestmsg);

dotest(tctestmsg);

end;

procedure tform1.button2click(sender: tobject);

begin

dotest(tetestmsg, true);

dotest(tctestmsg, true);

end;

end.

委託的示例

action 是乙個委託的方法,他是沒有返回值型別的,他是只適用於viod的函式。func 也是委託方法,他有有返回值型別的,注意 比如func這裡面你的三個int引數,前兩個代表引數型別,最後乙個永遠是返回值型別。觀察者模式與委託,事件 加上event只能類裡面觸發,不能再類的外部觸發。不加eve...

簡單的委託示例

定義乙個類,有兩個靜態方法,對double型別的值執行兩個操作,然後使用委託呼叫這些方法 class mathsoperations public static double multiptybytwo double value return value 2 public static double...

事件和委託示例

1 timer的elapsed事件 using system using system.collections.generic using system.linq using system.text using system.timers namespace timer的elapsed事件 e.si...