samedi 5 août 2023

Design pattern for handling different sets of inherited variables?

I have a base class that stores all columns of one record of a database. In that class I have set and methods as well. I also have a pure virtual method named Run(). Now I wan to have three child classes that send different fields of that record to different recepients using the Run() method. But, here's the problem. Child classes inherit variables (fields of the databse record in this example) that don't use. I only want to use only common fileds in the base class, and store the others in child classes. Then how would I set the member variables of child classes using the base class pointer? None of the Child1, Child2, Child3 uses all the field1, field2, .., fieldN. If transfer the fields that each child wants into its own class, then I how would I use SetFields from parent to set different fields in different child classes?

class Interface
{
public:
int field1, field2, field3, ..., fieldN;
void SetFields(field1,field2,field3,fieldN); // --\> gets all the fields as arguments
virtual void Run() = 0;
};

class Chid1: public Interface
{
void Run() {
/// send field1, field2, field3 to recepient1
}
};

class Chid2: public Interface
{
public:
void Run() {
/// send field4, field5, field6 to recepient2
}
};
class Chid3: public Interface
{
public:
void Run() {
/// send field7, field8, fieldN to recepient3
}
};
I

need design pattern that can set child members using parent class.

Aucun commentaire:

Enregistrer un commentaire