| View previous topic :: View next topic |
| Author |
Message |
Joe Otten Guest
|
Posted: Mon Jul 21, 2003 11:28 am Post subject: Design question (global variable) |
|
|
OK, we have 3 packages:
Package A is mine
Package B is third party or Borland or something - can't touch
Package C is mine.
A requires B. C requires A and B.
Currently I have a method in A which calls a method in B, which calls in
turn a method in C of a class that C has registered with B, which is a
subclass of a class that B knows about.
Problem: I want to pass an additional item of information from the class in
A to the class in C (a reference to itself would do).
Of course I could use a global variable (I didn't say it *just* to get your
attention), so in A
Assert(MyGlobal = nil);
MyGlobal := Self;
try
classinB.DoStuff;
finally
MyGlobal := nil;
end;
And I could protect it against threading problems if that was important.
But is there a good way to do it?
Joe
|
|
| Back to top |
|
 |
Joe Otten Guest
|
Posted: Mon Jul 21, 2003 2:41 pm Post subject: Re: Design question (global variable) |
|
|
| Quote: | You can solve this by adding a property to TClassC which could be set from
within TClassA.DoSomething:
|
That would work, except that ClassA never gets a reference to the instance
of ClassC that is doing the work.
Joe
|
|
| Back to top |
|
 |
John Elrick Guest
|
Posted: Mon Jul 21, 2003 3:32 pm Post subject: Re: Design question (global variable) |
|
|
"Joe Otten" <joe (AT) datator (DOT) co.uk> wrote
| Quote: |
OK, we have 3 packages:
Package A is mine
Package B is third party or Borland or something - can't touch
Package C is mine.
|
Don't call B directly...
Package MyClient is mine
Package ThirdParty is third party or Borland or something - can't touch
Package MyServer is mine.
ThirdParty.Method;
constructor TMyServer.Create(const aThirdParty: TThirdParty);
begin
FThirdParty := aThirdParty;
FThirdParty.RegisterMethod(CallbackMethod);
end;
procedure TMyServer.Method(const aClient: TMyClient);
begin
FThirdParty.Method;
...do something with client
So:
localServer := TMyServer.Create(aThirdParty);
or
localServer := TMyServer.Create(TThirdParty.Create);
localServer.Method(Self);
John
|
|
| Back to top |
|
 |
Bryan Crotaz Guest
|
Posted: Wed Jul 23, 2003 12:32 pm Post subject: Re: Design question (global variable) |
|
|
Declare an interface on classC, and query objectInB for it. If you find it,
it must be one of yours and you can use the interface to pass data to it.
Bryan
|
|
| Back to top |
|
 |
|