Interface programming in non-COM environment - Question, Tips, Applications (2)

zhaozj2021-02-16  42

(Connected to above)

Code:

Now, the code is now listed below, some of which have no code given above, but they are also very important. It is convenient for everyone to browse. Please take a closer look at the following code to get the information you need, of course, this article is just just A simple example, raised some common problems and solutions, as well as a possible application of interface programming like this.

Interface: ifoo = interface;

Ifoomanager = interface

['{3A10DC39-4B14-4C61-B657-E445C55408B6}']

Function Createafoo: IFOO;

Procedure delafoo (ID: integer);

Function Getfoonum: integer;

Function getfoobyid (ID: integer): IFOO;

END;

Ifoo = interface

// We have to maintain the object, only a simple addition operation as an example

['{22C541AA-0BA4-4092-B0EB-D267AB1FF001}']

Function FooAdd (X, Y: Integer): Integer;

END;

Tfoo's statement and implementation: Tfoo = Class (TMYINTERFAedObject, IFOO)

protected

Function FooAdd (X, Y: Integer): Integer;

END;

IMPLEMENTATION

{Tfoo}

Function TFOOAD (X, Y: Integer): Integer;

Begin

Result: = x y;

END;

Factory category and implementation:

TFOOManager = Class (TMYINTERFAedObject, Ifoomanager)

Private

Flist: array of tfoo;

Foonum: integer;

protected

Constructor crete;

Function Createafoo: IFOO;

Procedure delafoo (ID: integer);

Function Getfoonum: integer;

Function getfoobyid (ID: integer): IFOO;

public

DESTRUCTOR DESTROY; OVERRIDE;

END;

VAR

Fooman: tfoomanager;

IMPLEMENTATION

{Tfoomanager}

Constructor tfoomanager.create;

Begin

FOONUM: = 0;

END;

Function Tfoomanager.createafoo: Ifoo;

Begin

Inc (FOONUM);

if Length (Flist)

SETLENGTH (Flist, Foonum * 2);

FLIST [foonum-1]: = tfoo.create;

Result: = flist [fumbum-1] as ifoo;

END;

Procedure tfoomanager.delafoo (ID: integer);

VAR

i: integer;

Begin

IF foonum> 0 THEN

Begin

FLIST [ID] .free;

For i: = id to foonum-2 do

Begin

FLIST [I]: = flist [i 1];

END;

FLIST [FOONUM-1]: = NIL;

Dec (fumbum);

END;

END;

Destructor tfoomanager.destroy;

// Release all maintained object VAR before release the factory class

i: integer;

Begin

For i: = 0 to foonum-1 do

Begin

FLIST [I] .free;

FLIST [I]: = NIL;

END;

Finalize (FLIST);

inherited;

END;

Function tfoomanager.getfoobyid (ID: integer): IFOO;

Begin

Result: = flist [id] as impoo;

END;

Function Tfoomanager.getfoonum: integer;

Begin

Result: = foonum;

END;

There are only two export functions in the DLL:

Function Getfoomanintf: ifoomanager; stdcall;

Begin

IF not assigned (fooman) THEN

Begin

Fooman: = tfoomanager.create;

END;

Result: = foom as impoomanager;

END;

PROCEDURE FREELIB; STDCALL;

// Release factory class

Begin

IF assigned (fooman) THEN

Begin

Foom.Free;

Fooman: = NIL;

END;

END;

转载请注明原文地址:https://www.9cbs.com/read-27340.html

New Post(0)