This needs a different approach
Steven Schveighoffer
schveiguy at yahoo.com
Mon Apr 28 13:41:52 PDT 2008
"Saaa" wrote
>> You should seriously consider putting these things into classes instead
>> of modules, and looking up the right interface based on the data type
>> from a function. For example:
>>
>> interface Edible
>> {
>> void eat();
>> }
>>
>> class Pear : Edible
>> {
>> void eat() {...}
>> }
>>
>> static Edible[] fruits;
>>
>> static this()
>> {
>> fruits = [APPLE:new Apple(), PEAR:new Pear()...].dup;
>> }
>>
>> Edible get(uint type)
>> {
>> if(type > fruits.length)
>> return null;
>> return fruits[type];
>> }
>>
>> Robert is right, this is precisely the thing that OO programming was
>> created for :)
>>
>> -Steve
>
> I should have written this before, but data is actually an array.
>
> enum { APPLE, PEAR .. PLUM}
>
> for(i=0;i<1000;i++)
> {
> switch (data[i].type)
> {
> case APPLE:
> groupModule.appleModule.eat();
> break;
> case PEAR:
> groupModule.pearModule.eat();
> break;
> ..
> ..
> case PLUM:
> groupModule.plumModule.eat();
> break;
> default:
> break;
> }
> }
So with the get method I outlined above, the switch code becomes:
get(data[i].type).eat();
-Steve
More information about the Digitalmars-d-learn
mailing list