Answer the question
In order to leave comments, you need to log in
Why are long inheritance chains bad?
And so, the situation: We develop client-server applications, constantly work with a large number of entities and plan to use auto-generation of code based on object descriptions.
The standard apache thrift \ protobuf tools did not suit us, so we plan to write our own bike.
The generated code assumes that no manual modifications are made to it, which means that by definition there can be no logic there.
Our generated classes are actually a set of fields (property), given types, with getters / setters and serialization / deserialization methods.
In the standard case, our generated objects have some kind of inheritance chain C_GENERATED <- B_GENERATED A_GENERATED
We can easily further declare our C_CUSTOM <- C_GENERATED and add the logic we need.
But what to do if the logic needs to be declared in an intermediate, generated class, for example, in B_GENERATED (we may have several more classes inherited from B_GENERATED like CA_GENERATED <- B_GENERATED and the logic needs to be added to their parent class)?
There are two solutions:
1) generate an inheritance chain with intermediate custom classes: C_CUSTOM <- C_GEN <-B_CUSTOM <- B_GEN <- A_CUSTOM <- A_GEN
2) generate a symmetrical inheritance chain C_CUSTOM <- B_CUSTOM <- A_CUSTOM, and pass it to custom objects a link to the corresponding generated object. Approximately it might look like this:
class C_CUSTOM extends B_CUSTOM {
var _c;
override function setData(C_GEN c) {
parent::setData(c);
_c=c
}
}
class B_CUSTOM extends A_CUSTOM {
var _b;
function inc(int i) {
_b.aaa+=i;
}
override function setData(B_GEN b) {
parent::setData(b);
_b=b;
}
}
c_custom=new C_CUSTOM();
c_custom.setData(c_gen);
c_custom.inc(10);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question