lundi 30 novembre 2020

Advantage of using Factory method in C++

I was asked to create a Factory method and was asked to invoke the method "createEMRProcess" using factory method. Could anyone Please suggest any improvement or suggestions?

Below is the Factory Class and Factory method:

EMRFactory.h:

class CEMRFactory
{
public:
 std::shared_ptr<CTestImpl> createEMR(IMedOrder* f_hMedOrder, int f_nProductID, int f_nMedId);
};

EMRFactory.cpp:

#include "EMRFactory.h"

std::shared_ptr<CTestImpl> CEMRFactory::createEMR(IMedOrder* f_hMedOrder, int f_nProductID, int f_nMedId)
{
    return CTestImpl::getInstance(f_hMedOrder, f_nProductID, f_nMedId);
}

From the factory method, we are invoking the getInstance. And getInstance method is implemented in CTestImpl like as shown below:

TestImpl.h:

static shared_ptr<CTestImpl>               ms_hActiveInstance;

TestImpl.cpp

shared_ptr<CTestImpl> CTestImpl :: ms_hActiveInstance = NULL;

shared_ptr<CTestImpl> CTestImpl::getInstance(IMedOrder* f_hMedOrder, int f_nProductID, int f_nMedId)
{
    if (NULL == ms_hActiveInstance)
    {
        ms_hActiveInstance = shared_ptr<CTestImpl> (new CTestImpl(f_hMedOrder, f_nProductID, f_nMedId));
    }
    return ms_hActiveInstance;
}

I am using the Factory method in the below class like as shown below. And then invoking the method:

MainImpl.h

std::shared_ptr<CTestImpl> m_pTestImpl;
shared_ptr<CEMRFactory>   m_pEMRCollection;

MainImpl.cpp

void processApp()
{
 m_pTestImpl = m_pEMRCollection->createEMR(this, 1, 1);
 m_pTestImpl->createEMRProcess("EMR", Id, true, false);
}

Aucun commentaire:

Enregistrer un commentaire