[Gambas-user] 38 different classes - all exactly the same.

Fabián Flores Vadell fabianfloresvadell at ...626...
Tue Sep 4 23:21:54 CEST 2012


Hi Bruce.

> The roadblock I have now struck is this.  Each BO class requires a
> special "partner class" that contains the metadata as a static
> attribute. Each of these classes now looks exactly the same, same data
> items, same code.  The only difference is the class name.  Thus, 38
> different classes - all exactly the same.  But at runtime, obviously,
> each has a different metadata map loaded.

Looks like you haven't a good reason to create that 38 classes.

> What I'd like to be able to do is have only one of these, rather than
> 38.
>
> The reason that the metadata is held as a static attribute is that
> during any execution of a given program, there might be hundreds
> (sometimes thousands) of, say, Horse objects created.  So it makes sense
> to only load the metadata once and share it amongst all the BO's of that
> type.

I think you could move the shared behaviour to the Persistor class,
and use a class collection (defined as static private in Persistor) to
save the name of each of your current 38 classes, linked to the
references to the corresponding instances of BOMeta. In this way you
would have just one reference to a BOMeta object for each key in the
collection.

(If you find that the prev option isn't good enought, you could create
just one subclass of Persistor with the mentioned collection.)

In the next future, if you discover the need to have _p*BO classes
(i.e. if you found differents behaviours for they) you can add the
hierarchy of relevant classes.

> So, is there some way to create a class i.e. a new class not a new
> object, based on that common code at runtime?

I think  is impossible because Gambas isn't reflexive. I tried to hack
the Gambas features in order to do this, just for fun, but I failed
miserably :-(

Anyway, if would be possible to do, you will end with a kludge, and
you can't warrantee that the ugly hack will work in the next Gambas
release.

Regards.
Fabián.




More information about the User mailing list