Class hierarchy

Please ignore the syntax. I was thinking of making the post as brief as possible.

We have a class hierarchy as shown below.

// abstract class.   
class BaseProd   
// concrete classes.   
class Prod1 : public BaseProd   
class Prod2 : public BaseProd   
class Prod3 : public BaseProd   

Each class has 3 subclasses of type Prod1New, Prod1Cancel and Prod1Modify the same case for the remaining 2 classes. Now, since New, Cancel, and Modify are events, we can represent them using an enumeration.

The problem is that each class has many methods that are very specific to each event. For instance.

getRec(); // has specific implementations for each event.   

Let's say there is something like a process method.

void Prod1::process()
{
   getRec(); // Each derived class overrides the method.   
   getPayment();// Each derived class overrides the method.   
}  

Thus, we have 13 classes for 3 products, each of which has 3 events.
If we have 2 more products, classes will grow by 8.

Do we have an alternative approach to this hierarchy?

UPDATE: , , , Prod1 NewEvent Prod2 NewEvent . ? . .

+3
2

, New, Cancel Modify , . "is-a", . D B, "Every D B" . "A Prod1 - BaseProd" (.. " " ) - .

a Prod1Cancel a Prod1. : " 1 1". , Prod1Cancel , Prod1, , Prod1Cancel Prod1, .

BaseProd, , , :

class NewProductEvent {

   public:

     explicit NewProductEvent(BaseProd* product)
       : m_product(product)
     { /* ... */ }

     void getRec() { /* ... */ }
     void getPayment() { /* ... */ }

   private:

     BaseProd* m_product; // Use this to access the data that the event 
                          // needs from the product
};

Prod1::process() NewProductEvent :

void Prod1::process()
{
  NewProductEvent event(this);

  event.getRec();
  event.getPayment();
}
+3

. Prod .

class BaseProd
{
   public BaseProd(EventStrategy event) : _event(event);
   private EventStrategy _event;

   virtual public void Process();
}

void Prod1::process()
{
  _event.getRec(); // Each derived class overrides the method.   
  _event.getPayment();// Each derived class overrides the method.      
}   

EventStrategy overrode getRec(), getPayment() .. , , process() .

+2

Source: https://habr.com/ru/post/1756237/


All Articles