mardi 31 juillet 2018

Is below implementation of Abstract Factory Design Pattern is correct?

#include<iostream>

using namespace std;

//Abstract Product A

class Document
{

    public:

        virtual void createDocument() = 0;

};


class Word: public Document

{

    public :


        virtual void createDocument()
        {

            cout<<"Document Word is created"<<endl;         

        }
};


class Excel : public Document
{
    public :

        virtual void createDocument()
        {
            cout<<"Document Excel is created"<<endl;            
        }
};

//Abstract Product B


class VideoFile
{

    public:

        virtual void createVideoFile() = 0;

};


class VLC : public VideoFile
{

    public :

        virtual void createVideoFile()
        {
            cout<<"VideoFile VLC is created"<<endl;         

        }
};


class AVI : public VideoFile
{

    public :

        virtual void createVideoFile()
        {

            cout<<"VideoFile AVI is created"<<endl;         

        }
};



//////////////////////////////////////////////////////////////////////


class AbstractFactory

{

    public:

        virtual Document* createDocFiles(string fileType) = 0;

        virtual VideoFile* createVideoFiles(string fileType) = 0;       

};


class ConcreteFactoryA : public AbstractFactory
{

    public:

        virtual Document* createDocFiles(string fileType)
        {

            if(fileType == "Word")
                return new Word();
            if(fileType == "Excel")
                return new Excel();
        }

        virtual VideoFile* createVideoFiles(string fileType)
        {

            if(fileType == "VLC")
                return new VLC();
            if(fileType == "AVI")
                return new AVI();
        }

};


class ConcreteFactoryB : public AbstractFactory
{

    public:

        virtual Document* createDocFiles(string fileType)
        {

            if(fileType == "Word")
                return new Word();
            if(fileType == "Excel")
                return new Excel();
        }

        virtual VideoFile* createVideoFiles(string fileType)
        {

            if(fileType == "VLC")
                return new VLC();
            if(fileType == "AVI")
                return new AVI();
        }

};



int main()
{

    AbstractFactory *ptrAbFac = new ConcreteFactoryB();

    Document *ptrDoc =  ptrAbFac->createDocFiles("Excel");

    ptrDoc->createDocument();

    VideoFile  *ptrVideoFile = ptrAbFac->createVideoFiles("VLC");

    ptrVideoFile->createVideoFile();

}

Aucun commentaire:

Enregistrer un commentaire