-3

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();
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 3
    You don't have to have a static method though note your code has undefined behaviour as `accesFact` is an uninitialised pointer. If a method doesn't access any member variables it might as well be static – Alan Birtles May 05 '20 at 08:59
  • It depends, now you have to create a Factory only for creating a Shape. You could use singleton pattern for the factory, but a static method is in my eyes easier to understand and maintain, because it has lesser side effects – RoQuOTriX May 05 '20 at 09:02
  • No requirement to make methods static; also no requirement to make the factory allocated on the heap. You could also use: `Factory accesFact; Shape* obj1=accesFact.getShapeType(RECTANGLE_TYPE);` (benefits include no memory to manage for the factory). – Phil Brubaker May 05 '20 at 09:41
  • related: https://stackoverflow.com/questions/60276277/is-it-forbidden-to-use-static-methods-in-factory-pattern – jaco0646 May 05 '20 at 13:11

2 Answers2

2

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

There is no requirement like this. If you implement it that way, you can do this like you want.

But you should think of:

If you must create an object of a factory which have no instance data, why we should have that instance for nothing? It only wastes memory because the size of an instance is always bigger than zero by definition.

Make it a singleton is especially not helpful in this case, as we than guarantee that we have only one instance we still do not need. And the bad side of a singleton is, that it typically checks on every call if it is already instantiated which wastes additional resources.

In general, not only for a factory, if a class is only used to structure the code and contains no data, all methods can be static. Even if a class has data members, but a single method of such a class did not use any of that members, make it static as this avoids pushing the this pointer on the stack without any usage ( maybe compiler will optimize this away ).

There is not the one and only factory pattern and also not the one and only implementation option. But if you refer to the typical GOF factory pattern, there is something more than only create an object. The GOF factory enables you to create different objects depending on the active instance of the factory. The idea of the factory pattern is not creating different objects on a selector variable like your enum, but create complete sets of objects depending on the instance of the factory by keeping the interface the same. You completely missed that in your example. And as you did, it still did not require any data which makes your class empty and as this all methods should be static. If you use the GOF pattern, you have at minimum the vtable pointer as data member and virtual methods for the creating methods.

Klaus
  • 24,205
  • 7
  • 58
  • 113
0

Static method is not a requirement, but as long as you don't use non-static members of the class, method can be static.

Jarod42
  • 203,559
  • 14
  • 181
  • 302