Aggregation

COM aggregation is an object oritented design concept to take the instance of external/third party object and use it for the purpose of the co-classes in the server. This is a reuse of existing implementation without implementing by own. Here in the below example we are using a CExtEngine external class in CVehicle. We are not implemening CExtEngine but CVehicle is using its functionality by creating one instance.

class CExtEngine
{
public:
  static CExtEngine * CreateInstance();
  virtual int Release() = 0;
  virtual int PowerOff() = 0;
  virtual int SetGear(int nGear) = 0;
  virtual int Drive() = 0;
  virtual int Reverse() = 0;

};
class  CVehicle
{

private:
    int m_nGear;
    CExtEngine *m_pEngine;
public:
    CVehicle()
    {
    m_nGrear = GEAR_NUTRAL;
    m_pEngine = CExtEngine::CreateInstance();
    
    }
    ~CVehicle()
    {
    m_nGrear = GEAR_NUTRAL;
    m_pEngine->PowerOff();
    m_pEngine->Release();
    m_pEngine = NULL;
    
    }
    void ChangeGear(int nGear)
    {
      m_nGrear = nGear;
      if(m_nGrear == GEAR_NUTRAL)
      {
        m_pEngine->PowerOff();
      }
      else {
        m_pEngine->SetGear(m_nGrear);
      }
    }
    int Drive()
    {
      m_pEngine->SetGear(GEAR_DRIVE);
      return m_pEngine->Drive();
      
    }
    int Reverse()
    {
      m_pEngine->SetGear(GEAR_DRIVE);
      return m_pEngine->Reverse();
      
    }
};

Containment

Containment also an object oritented design concept by including or inheriting an module in the class. This very similar to aggregation but here we implement the module and reuse it by creating a member object in the derived class or inheriting the base class to derived class.

class CEngine
{

private:
    int m_nGear;
public:
    CEngine()
    {
    m_nGrear = GEAR_NUTRAL;
    
    }
    int Drive()
    {
      
    }
    int Reverse()
    {
      
    }
};

class CVehicle : public CEngine 
{

private:

public:
    CVehicle(): CEngine()
    {
      
    }
    ~CVehicle()
    {
    
    }

};

About our authors: Team EQA

You have viewed 1 page out of 67. Your COM/DCOM learning is 0.00% complete. Login to check your learning progress.

#