Is there an OO pattern to fill objects with lots of data, with this data being usually different within each object, polymorphically?
- My first idea was to create a base class with pure virtual methods. So each concrete object would implement how its data is filled. However, as the code evolved I noticed that the base class grown too much only to store dozens of pure abstracts methods, and that concrete classes overwritten a lot of unnecessary methods that were only useful in its siblings.
Example:
public Filler
{
virtual void fill_struct1(struct myStruct1) = 0;
virtual void fill_struct2(struct myStruct2) = 0;
// I will need more methods to give the derivate objects the capacity of
// filling its data members polymorphically.
}
class A: public Filler
{
void fill_struct1(struct myStruct1);
void fill_struct2(struct myStruct2);
// There can be more overwritten methods.
struct myStruct1 member_1;
struct myStruct2 member_2;
struct myStruct3 member_3;
// There can be more members of different types.
}
class A: public Filler
{
void fill_struct1(struct myStruct1);
void fill_struct2(struct myStruct2);
void fill_struct4(struct myStruct4);
// There can be more overwritten methods.
struct myStruct1 member_1;
struct myStruct2 member_2;
struct myStruct4 member_3;
// There can be more members of different types. But sometimes the members can be equal one used in a sibling.
}
Aucun commentaire:
Enregistrer un commentaire