I´m working on a project that deals with a huge amount of different kinds of objects retrieved from a database. For further considerations let´s suppose we have a much simpler structure as follows (for simplicity access-modifiers and such are omited):
class Outer {
Mid mid;
string someString;
void createMid(string str) {
// some work
this.mid = new Mid(str);
}
}
class Mid {
Inner inner;
string someOtherString;
}
class Inner {
string innerString;
}
class Inner2 : Inner {
// some custom behaviour for innerString
}
Now we create an instance of Outer
that also instantiates its nested objects. Now we want to change some behaviour within the Inner
-class by subclassing it (let´s say we want to set its innerString
-property to some different value than in its base-implementation). My current approach for this is to subclass Inner
. To create an instance of this derived class we also have to subclass Mid
, whereby we also subclass Outer
. Whilst creating this nested structure consists of many operations in the Outer
-object we thus should also override most of our code only to instantiate Inner2
-instance and thus also another instance of Mid
.
I know that sounds like a huge design-flaw but I don´t come to any other solution for this kind of design than subclassing the whole structure and overriding the whole createMid
-method just for this relative small sub-object.
Is there any other approach for subclassing Inner
without also subclassing all the others?