Registriert seit: 2. Jul 2007
Ort: Tuttlingen
81 Beiträge
Delphi 10.1 Berlin Professional
|
Re: Zurück greifen auf "Mutter-Objekt"
10. Jun 2008, 17:19
Wie wäre es mit Events?
Delphi-Quellcode:
unit Unit1;
interface
uses
Classes, Unit2;
type
TObjectA = class(TObject)
private
FB: TObjectB;
procedure FB_OnDone(Sender: TObject);
public
constructor Create;
procedure Work;
end;
implementation
constructor TObjectA.Create;
begin
inherited Create;
FB := TObject.Create;
FB.OnDone := FB_OnDone;
end;
procedure TObjectA.FB_OnDone(Sender: TObject);
begin
// Zugriff auf Methoden oder Eigenschaften von FB, z.B.:
if FB.Success then
begin
// ...
end;
end;
procedure TObjectA.Work;
begin
FB.Run;
end;
Delphi-Quellcode:
unit Unit2;
interface
uses
Classes;
type
TObjectB = class(TObject)
private
FOnDone: TNotifyEvent;
FSuccess: Boolean;
procedure Done(Sender: TObject);
public
procedure Run;
property Success: Boolean read FSuccess;
property OnDone: TNotifyEvent read FOnDone write FOnDone;
end;
implementation
procedure TObjectB.OnDone;
begin
if Assigned(FOnDone) then
FOnDone(Self);
end;
procedure TObjectB.Run;
begin
// Mache irgendwas ...
FSuccess := True;
Done;
end;
Dadurch muss TObjectB nichts von TObjectA wissen und Du hast die beiden Klassen soweit wie möglich entkoppelt.
jkr
Jürgen Krämer Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)
|