aaaa
2011-12-27 21:14:35 UTC
Hi,
I have classes:
TDog, TCat, TFish.
Each one has a function named sing().
Each one should register itself in TManager class, like this:
TManager.Register('Dog', TDog);
Now I want to supply a string to TManager class to make the animal sing(),
like this:
m := TManager;
m.sing('Dog'); //should call TDog's sing() function.
Now the hard part:
1) I want the animals to register themself, so I don't want any ifs like
this in TManager:
if str = 'Dog' then TDog.sing();
I want to be able to remove TDog's unit from uses and I want all my code to
work fine with all the animals that are left.
2) The animals do not have a common base class, so I cannot do something
like:
type TAnimalClass = class of TAnimal;
var a: TAnimal;
ac: TAnimalClass;
begin
a := ac.Create;
a.sing();
How to solve this problem?
Best regards,
A
I have classes:
TDog, TCat, TFish.
Each one has a function named sing().
Each one should register itself in TManager class, like this:
TManager.Register('Dog', TDog);
Now I want to supply a string to TManager class to make the animal sing(),
like this:
m := TManager;
m.sing('Dog'); //should call TDog's sing() function.
Now the hard part:
1) I want the animals to register themself, so I don't want any ifs like
this in TManager:
if str = 'Dog' then TDog.sing();
I want to be able to remove TDog's unit from uses and I want all my code to
work fine with all the animals that are left.
2) The animals do not have a common base class, so I cannot do something
like:
type TAnimalClass = class of TAnimal;
var a: TAnimal;
ac: TAnimalClass;
begin
a := ac.Create;
a.sing();
How to solve this problem?
Best regards,
A