mercredi 28 juin 2023

C How to make use of a fixed hierarchy of classes for various similar entities like employee-Manager for company 123

Difficult to give appropriate title to this question Let me explain the scenario.

I have a class say Employee and a subclass (of Employee) say Manager.

There are various methods (and data member) in Employee class related to payroll, employee details, appraisal, etc. Similarly for Manager there are various methods.

class Employee {
    string name, address, empId;
    int salary;
    Dept dept;
public:
    void [Get/Set]Name();
    void GetEmpid();
    void SetSalary();
    virtual void StartOnboarding();
    virtual void startOfboarding();
    virtual void startAppraisal();
};

class Manager : public Employee {
    list teamList;
    int appraisalBudget;    
public:
    int [Get/Set]AppraisaBudget();
    int [Add/Delete]EmployeeInTeam(emp);
    void StartTeamAppraisal();
};

Scenario is that I have many companies says Company1, Company2, etc. and more can be added in future. An employee or manager can be of company 1, company2, etc.

Employee and Manager for company1 will have similar relationship as above, same for company2. But they will have different ways to handle methods available. Like startOnboarding() for employee of company1 will be different than employee of company2, similarly StartTeamAppraisal() for manager of company1 will be different than manager of company2.

Now one way to model this scenario is to create different sub-classes of Employee for every company like EmployeeCompany1, EmployeeCompany2, and similarly sub-classes of Manager for every company like ManagerCompany1, ManagerCompany2, etc.

Employee classes for different company -

class EmployeeCompany1 : public Employee {
    int tasksDone;
    int clientsMeetingsDone;
    int appreciationsReceived
public:
        // Company1 specific implementation of following
    virtual void StartOnboarding() { ... }
    virtual void startOfboarding() { ... }
    virtual void startAppraisal()  { ... }
};

class EmployeeCompany2 : public Employee {
    int bugSolved;
    int featureDeveloped;
public:
        // Company2 specific implementation of following
    virtual void StartOnboarding() { ... }
    virtual void startOfboarding() { ... }
    virtual void startAppraisal()  { ... }
};

But in above case EmployeeCompany[1,2,..] will be subclass of Employee and Manager is already a subclass of Employee but they both(EmployeeCompany[1,2..] and Manager) are not on the same level as per the behaviour is considered. So there is some flaw in the design.

If we do similar for ManagerCompany1, than It has to be a subclass of EmployeeCompany1, but it also has to be a subclass of Manager, like below -

class ManagerCompany1 : public EmployeeCompany1, public Manager {
protected:
    int company1specificData;
public:
    virtual void StartTeamAppraisal() {
    // make use of data member of EmployeeCompany1 and other things specific to company1
    }
};


class ManagerCompany2 : public EmployeeCompany2, public Manager {
protected:
    int company2specificData;
public:
    virtual void StartTeamAppraisal() {
    // make use of data member of EmployeeCompany2 and other things specific to company2
    }
};

  1. I feel my design is having flaws and the scenario I described would be a well defined scenario in object oriented design literature.

  2. I am looking for community's help for a better design approach to model above scenario in c++.

  3. May be You can also suggest a better title for this question.

Aucun commentaire:

Enregistrer un commentaire