spoxta
08-09-2007, 04:45 PM
Hi,
I am not sure if this is in line with any OO programming principles,
but I came across a need, so I wanted to check if this is possible
and/or receive any recommendations how to approach this.
Here is the situation.
I have base classes
BaseCalculate and
BaseData
and I have various derived classes from both, like:
FooCalculate and
FooData
BarCalculate, BarData
BazCalculate, BazData
all the common methods and attributes of the implemented algorithms
are bundled in the Base classes.
Each Calculate class has attributes to hold objects of its
corresponding Data counterpart. In the BaseCalculate class there are
methods that actually do some work on these data objects (but only
interface with what is provided by the BaseData class).
The actual calculation algorithms use typically additional methods
from the derived Data classes.
Right now, to implement this I declared a protected variable in BaseCalculate like:
protected var _mydata:BaseData;
But in (e.g.) FooCalculate, this variable is not assigned a BaseData object,
but a FooData object, like:
_mydata = new FooData;
so far so good, all ok, since FooData is derived from BaseData is can
be assigned.
Now the ugly part is, that whenever I need to call specific methods from
FooData, BarData or BazData which are not in BaseData I have to
explicitly tell that the object in _mydata is not only of type BaseData but
actuall FooData, meaning I write a lot of code like this:
(_mydata as FooData).fooDataSpecificFunction();
What I would like instead is a way to tell the FooCalculate class that the
inherited _mydata attribute is actually of type FooData, so that I do not
need to do it everywhere in the code individually.
Any ideas how to do this??
My naive approach would have been to override the declaration of _mydata
with the more specific type, but of course this does not work, but maybe
there is a way to achieve it anyway.
I also tried to declare _mydata private and redeclare it in derived
class with the specific type, but this leads to having two separate
references with the one in BaseCalculate uninitialised....
Hmmm now thinking of it, I could write a protected setter in BaseCalculate
and when the specific object is created it will be set in both variables
(which remain private). That could work....
I am not sure if this is in line with any OO programming principles,
but I came across a need, so I wanted to check if this is possible
and/or receive any recommendations how to approach this.
Here is the situation.
I have base classes
BaseCalculate and
BaseData
and I have various derived classes from both, like:
FooCalculate and
FooData
BarCalculate, BarData
BazCalculate, BazData
all the common methods and attributes of the implemented algorithms
are bundled in the Base classes.
Each Calculate class has attributes to hold objects of its
corresponding Data counterpart. In the BaseCalculate class there are
methods that actually do some work on these data objects (but only
interface with what is provided by the BaseData class).
The actual calculation algorithms use typically additional methods
from the derived Data classes.
Right now, to implement this I declared a protected variable in BaseCalculate like:
protected var _mydata:BaseData;
But in (e.g.) FooCalculate, this variable is not assigned a BaseData object,
but a FooData object, like:
_mydata = new FooData;
so far so good, all ok, since FooData is derived from BaseData is can
be assigned.
Now the ugly part is, that whenever I need to call specific methods from
FooData, BarData or BazData which are not in BaseData I have to
explicitly tell that the object in _mydata is not only of type BaseData but
actuall FooData, meaning I write a lot of code like this:
(_mydata as FooData).fooDataSpecificFunction();
What I would like instead is a way to tell the FooCalculate class that the
inherited _mydata attribute is actually of type FooData, so that I do not
need to do it everywhere in the code individually.
Any ideas how to do this??
My naive approach would have been to override the declaration of _mydata
with the more specific type, but of course this does not work, but maybe
there is a way to achieve it anyway.
I also tried to declare _mydata private and redeclare it in derived
class with the specific type, but this leads to having two separate
references with the one in BaseCalculate uninitialised....
Hmmm now thinking of it, I could write a protected setter in BaseCalculate
and when the specific object is created it will be set in both variables
(which remain private). That could work....