unit model;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, jpeg, StdCtrls, pngimage, JvPanel, JvExExtCtrls,
JvExtComponent, Series, TeeShape, TeEngine, TeeProcs, Chart, Math, TeeGDIPlus,
ComCtrls, JvExComCtrls, JvComCtrls, dOPCIntf, dOPCComn, dOPCDA, dOPC;
type
TEvent =
procedure of object;
TModel =
class
protected
// interne Liste
OnChange:
array of TEvent;
// Aufruf aller Routinen der Liste
procedure notify;
destructor destroy;
override;
public
// neuer 'Event-Handler' in Liste
procedure registerOn(routine: TEvent);
// 'Event-Handler' aus Liste entfernen
procedure registerOff(routine: TEvent);
end;
implementation
// registriert neue routinen an den controller
procedure TModel.registerOn(routine: TEvent);
var
n: integer;
begin
n := Length(OnChange);
SetLength(OnChange, n + 1);
OnChange[n] := routine;
end;
// de-registriert routinen vom controller
procedure TModel.registerOff(routine: TEvent);
var
i, j: integer;
begin
i := Low(OnChange);
while i <= High(OnChange)
do // High liefert -1 bei leerem Array
begin
if @OnChange[i] = @routine
// mit '@' nur Adressen vergleichen
then
begin
for j := i
to High(OnChange) - 1
do
OnChange[j] := OnChange[j + 1];
SetLength(OnChange, Length(OnChange) - 1);
end
else
i := i + 1;
end;
end;
// alle Ereignis-Behandlungs-Routinen der Liste aufrufen
destructor TModel.destroy;
begin
//
inherited;
end;
procedure TModel.notify;
var
i: integer;
begin
for i := Low(OnChange)
to High(OnChange)
do
OnChange[i];
end;
end.