Einzelnen Beitrag anzeigen

Roachford
(Gast)

n/a Beiträge
 

Re: Änderungen in TList verfolgen

  Alt 10. Sep 2008, 12:51
Delphi-Quellcode:
  TListNotifyEvent = procedure(AList: TObject; const AAction: TListNotification; const AElement: TObject) of object;
  TListDescendant = class(TList)
  private
    FOnNotify: TListNotifyEvent;
  protected
    procedure Notify(Ptr: Pointer; Action: TListNotification); override;
  public
    property OnNotify: TListNotifyEvent read FOnNotify write FOnNotify;
  end;

...

procedure TListDescendant.Notify(Ptr: Pointer; Action: TListNotification);
begin
  if assigned(FOnNotify) then
    FOnNotify(Self, Action, TObject(Ptr));

  inherited;
end;
Delphi-Quellcode:
  TMyGrid = class(TCustomGrid)
  private
    fFilteredList: TListDescendant;
    fOtherList: TListDescendant;

  protected
    Procedure ListNotifier(AList: TObject; const AAction: TListNotification; const AElement: TObject);

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

...

constructor TMyGrid.Create(AOwner: TComponent);
begin
  inherited;

  fFilteredList := TListDescendant.Create;
  fFilteredList.OnNotify := ListNotifier;
  fOtherList := TListDescendant.Create;
  fOtherList.OnNotify := ListNotifier;
end;

destructor TMyGrid.Destroy;
begin
  fFilteredList.Free;
  fOtherList.Free;

  inherited;
end;

procedure TMyGrid.ListNotifier(AList: TObject; const AAction: TListNotification; const AElement: TObject);
begin
  if AList = fFilteredList then
    // gefilterte Liste
  else if AList = fOtherList then
    // die andere Liste

  ODER

  Liste := TListDescendant(AList);
end;
Es gibt auch noch die Möglichkeit, dass die Liste direkt wissen von TMyGrid hat und direkt von diesem z.B. eine Methode aufruft über das friend-Verhalten innerhalb der Unit, aber das ist zum einen schlechter Stil und zum anderen kann man dann schlecht erweitern, also mal ein abgeleitetes TMyGrid einsetzen etc. Da sind die Eventhandler deutlich flexibler und es werden keine OOP Konzepte eingerissen (wie bei Friend-Prinzip) - ausser natürlich durch die Closure's an sich, welche CodeGear genau dazu eingeführt hat.
  Mit Zitat antworten Zitat