mardi 5 mai 2020

why do we really require static function in factory design pattern?

we can do the same with normal member function in factor class and can access with factory pointer variable? can some one please tell the difference.

enum shapeType
{
    CIRCLE_TYPE,
    RECTANGLE_TYPE,
    SQUARE_TYPE
};

class Shape
{
    public:
    virtual void draw()=0;
    virtual ~Shape(){

    }
};

class Rectangle:public Shape
{
    public:
    void draw()
    {
        cout<<" draw rectangle"<<endl;
    }
};

class Square:public Shape
{
    public:
    void draw()
    {
        cout<<" draw square"<<endl;
    }
};

class Circle:public Shape
{
    public:
    void draw()
    {
        cout<<" draw circle"<<endl;
    }

};



 class Factory //comes with library
    {
        public:
          /*  static Shape *getObject(shapeType type)
            {
                if (type == CIRCLE_TYPE)
                    return new Circle;
                if (type == RECTANGLE_TYPE)
                    return new Rectangle;
                if (type == SQUARE_TYPE)
                    return new Square;
            }*/

      Shape* getShapeType(shapeType type)
        {
               if(type==CIRCLE_TYPE)
                  return new Circle;
               if(type==RECTANGLE_TYPE)
                  return new Rectangle;
               if(type==SQUARE_TYPE)
                  return new Square;

        }

    };

int main()
{   
    Factory *accesFact;
    Shape* obj1=accesFact->getShapeType(RECTANGLE_TYPE);
    obj1->draw();
}

Aucun commentaire:

Enregistrer un commentaire