is-Logo

Objektorientierte Programmierung (OOP)
M-V-C Implementierung - Observer Pattern Design

S. Spolwig


[Home | Programmiersprachen | Delphi]
 

unit uObserverListe;
(* ******************************************************************** *)
(* K L A S S E : TObserverListe                                         *)
(* -------------------------------------------------------------------- *)
(* Version     : 1.0                                                    *)
(* Autor       : S. Spolwig, OSZ-Handel I, 10997 Berlin                 *)
(*               nach Christer Höstklint, Fredrik Öhman                 *)
(* Aufgabe     : Liste für die ObserverItems.                           *)
(* Compiler    : DELPHI 7.0                                             *)
(* Aenderung   : V. 1.0   14-OKT-04                                     *)
(* ******************************************************************** *)
interface
(* ==================================================================== *)
uses
   contnrs,
   classes,
   uObsItem;

type
  TObserverListe = class(TObject)
  private
    FObserved: TObject;
    FObserverList: TObjectList;
    function FindMatch(aNotifyEvent: TNotifyEvent; Observer: TObject): Integer;
  public
    constructor Create(Owner : TObject);
    procedure AddObserver(aNotifyEvent : TNotifyEvent ; Observer : TObject);
    procedure RemoveObserver(aNotifyEvent : TNotifyEvent ; Observer : TObject);
    procedure RunObservers;
  end;
  
implementation
(* ==================================================================== *)

constructor TObserverListe.Create(Owner : TObject);
(* -------------------------------------------------------------------- *)
begin
  inherited Create ;
  FObserved := Owner ;
  FObserverList := TObjectList.Create;
  FObserverList.OwnsObjects := true;
end;

procedure TObserverListe.AddObserver(aNotifyEvent : TNotifyEvent ; Observer : TObject);
(* -------------------------------------------------------------------- *)
var
  anItem: TObserverItem;
begin
  if (0 > FindMatch(aNotifyEvent, Observer)) then
    begin
    anItem := TObserverItem.Create(FObserved) ;
    anItem.OnChange := aNotifyEvent ;
    anItem.Observer := Observer ;
    FObserverList.Add(anItem) ;
    end ;
end;

function TObserverListe.FindMatch(aNotifyEvent: TNotifyEvent; Observer: TObject): Integer;
(* -------------------------------------------------------------------- *)
var
  I: Integer;
  anItem: TObserverItem;
begin
  result := -1 ;
  
  For I := 0 to FObserverList.count-1 do
    begin
    anItem := TObserverItem( FObserverList.Items[i] ) ;
  
    if anItem.Observer = Observer then
      begin
      if @aNotifyEvent = nil then
        begin
        result := I ;
        exit ;
        end
      else if (@anItem.OnChange = @aNotifyEvent) then
        begin
        result := I ;
        exit ;
        end ;
      end ;
    end ;
end;

procedure TObserverListe.RemoveObserver(aNotifyEvent : TNotifyEvent ; Observer : TObject);
(* -------------------------------------------------------------------- *)
var
  I: Integer;
begin
  repeat
   I := FindMatch(aNotifyEvent, Observer) ;
   if (I >= 0) then FObserverList.Delete(I) ;
  until (I < 0) ;
end;

procedure TObserverListe.RunObservers;
(* -------------------------------------------------------------------- *)
var
  I: Integer;
  anObserver: TObserverItem;
begin
  For I := 0 to FObserverList.Count - 1 do
    begin
    anObserver := TObserverItem( FObserverList.items[i] );
    anObserver.Change ;
    end ;
end;

end.

 


 

©    04. Oktober 2008     Siegfried Spolwig

Seitenanfang