Re: D5 - Class\Collection example??
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Sun, 14 Oct 2007 17:09:09 +0200
"Tom de Neef" <tdeneef@xxxxxxxx> wrote in message
news:47113ea2$0$232$e4fe514c@xxxxxxxxxxxxxxxxx
[...]
// what to do about the partner of this partner ?
// It should be self, but you cannot call partner.addPartner(self)
// since it will result in an infinite loop !
Allow me to answer that (possibly rhetorical) question.
The trick is to break the endless loop when you recognise that the
situation has stabilised. You set your own Partner, the Partner sets
you as their partner, you set your own partner to them again, except
whenever you're not changing anything thereby, you stop.
Properties let you do this.
type
TPerson = class(TPersistent) { I like being able to Assign }
private
FPartner: TPerson;
protected
procedure SetPartner(const Value: TPerson); virtual;
function DoPartnerChanging(var NewValue: TPerson); virtual;
procedure DoPartnerChanged; virtual;
public
property Partner: TPerson read FPartner write SetPartner;
end;
....
procedure TPerson.SetPartner(const Value: TPerson);
var NewValue: TPerson;
begin
NewValue:=Value;
if (not (Partner=Value)
and DoPartnerChanging(NewValue)
and not (Partner=NewValue)
)
then begin
FPartner:=NewValue;
DoPartnerChanged;
end;
end;
function TPerson.DoPartnerChanging(var NewValue: TPerson): Boolean;
begin
Result:=not Assigned(Partner) or Partner.ConsentsToBreakup;
if (Result
and Assigned(Partner)
and (Partner.Partner=Self)
)
then Partner.Partner:=nil;
end;
procedure TPerson.DoPartnerChanged;
begin
if (Assigned(Partner))
then begin
Partner.Partner:=Self;
if not (Partner.Partner=Self)
then Partner:=nil;
end;
end;
The logic is complicated by the possibility of the prospective partner
refusing to enter into the relationship. Then you have to roll back your
own Partner assignment, and the forwards and backwards pointers are
momentarily inconsistent. This is why the OnChanging event dispatcher
has such an extensive test before breaking up the current relationship.
The third term was an Assertion inside the if statement, but I think it
would have failed if the reversion code in the OnChanged event dispatcher
ever executed.
The social consequences of this code are left as an exercise to the
reader.
Groetjes,
Maarten Wiltink
.
- Follow-Ups:
- Re: D5 - Class\Collection example??
- From: Don
- Re: D5 - Class\Collection example??
- From: Hans-Peter Diettrich
- Re: D5 - Class\Collection example??
- From: Tom de Neef
- Re: D5 - Class\Collection example??
- From: Maarten Wiltink
- Re: D5 - Class\Collection example??
- References:
- D5 - Class\Collection example??
- From: Don
- Re: D5 - Class\Collection example??
- From: Tom de Neef
- D5 - Class\Collection example??
- Prev by Date: Component for reading DNG files
- Next by Date: Re: D5 - Class\Collection example??
- Previous by thread: Re: D5 - Class\Collection example??
- Next by thread: Re: D5 - Class\Collection example??
- Index(es):
Relevant Pages
|