Thread: factory pattern using static

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    216

    factory pattern using static

    Can anyone tell me why factory pattern depend on static ? is it because static inheritance allows objects to be instantiated or static member data can be shared across all instances or static member function doesn't need to be called from an instance of their own class or static data at function scope persists between function calls here?

    Code:
    class  Vehcle{ 
    public: 
        virtual void displayVehcle() = 0; 
        static Vehicle* Create(VehcleType type); 
    }; 
    class TwoWheeler : public Vehcle { 
    public: 
        void printVehcle() { 
            cout << "two wheeler" << endl; 
        } 
    }; 
    class ThreeWheeler : public Vehcle { 
    public: 
        void printVehcle() { 
            cout << "three wheeler" << endl; 
        } 
    }; 
    class FourWheeler : public Vehcle { 
        public: 
        void printVehcle() { 
            cout << "four wheeler" << endl; 
        } 
    }; 
      
    Vehcle* Vehcle::Create(VehcleType type) { 
        if (type == VT_TwoWheeler) 
            return new TwoWheeler(); 
        else if (type == VT_ThreeWheeler) 
            return new ThreeWheeler(); 
        else if (type == VT_FourWheeler) 
            return new FourWheeler(); 
        else return NULL; 
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    The static keyword here just designates the member function as a static member function. You could just as easily use a non-member function, except that it makes sense to associate the function more closely with the class hierarchy by making it a static member function of the base class.

    Note that the base class should have a virtual destructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    216
    Did you mean to say that is it because static inheritance allows objects to be instantiated or static member data can be shared across all instances or static member function doesn't need to be called from an instance of their own class or static data at function scope persists between function calls here?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    Err... the fact that you're listing four options that are rather different is rather weird. It is almost as if this is some MCQ question for a homework assignment and you're hoping someone will just spit out the correct answer so you can write it down without understanding. In fact, you don't even need to truly understand my post #2 to use it to zoom in on one of the options.

    Anyway, let's work on your understanding: for each of the four options, provide a code example and explain what it means.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    216
    This is not MCQ, I suspected those could be the reasons. My strong suspect is that static member function doesn't need to be called from an instance of their own class . i am not a design pattern expert.

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    69
    Correct.

    A factory method is a method that is called to build an instance of a class (or subclass). For this reason, you don't usually call factory methods using a class instance. Rather, you make the factory method a static method and call against the class directly.

    Code:
    new_instance = instance.factory(args);   // NO!
    
    new_instance = Class::factory(args);    // YES!

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    Quote Originally Posted by leo2008
    This is not MCQ, I suspected those could be the reasons.
    But how? The code that you provided does not have any static data members (or any data members at all) so "static member data can be shared across all instances" is obviously not a reason. There are no static local variables either, so "static data at function scope persists between function calls" is also obviously not a reason.

    I don't think "static inheritance" is defined in C++ parlance, but in any case inheritance mechanisms aren't what allows objects to be instantiated, so that can be ruled out as well.

    Quote Originally Posted by leo2008
    My strong suspect is that static member function doesn't need to be called from an instance of their own class .
    This leaves this reason involving static member functions, and indeed there are static member functions in the code you provided. What you probably meant to ask is why this implementation of the factory pattern uses static member functions rather than non-static member functions, in which case this reason is correct: you want to be able to call it without already having a Vehicle object, or you could end up with the situation of needing an object to create an object.

    However, this doesn't mean that "factory pattern depend on static". As I pointed out in post #2, you can use a non-member function instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    793
    Quote Originally Posted by aghast View Post
    Correct.

    A factory method is a method that is called to build an instance of a class (or subclass). For this reason, you don't usually call factory methods using a class instance. Rather, you make the factory method a static method and call against the class directly.

    Code:
    new_instance = instance.factory(args);   // NO!
    
    new_instance = Class::factory(args);    // YES!
    To be fair, you could have a factory that creates objects from an instance. Suppose you want to create objects with the same complex set of arguments; you could have an instance that contains the arguments, and then you just have to ask that instance to create a new object for those arguments. For example:

    Code:
    MyFactory* f = new MyFactory();
    // set a bunch of arguments
    f
      .setFoo(foo)
      .setBar(bar)
      .setBuzz(buzz);
    
    MyObject* obj = f.newObject();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple factory pattern
    By serge in forum C++ Programming
    Replies: 7
    Last Post: 11-19-2019, 03:18 PM
  2. Question about the Factory pattern.
    By h3ro in forum C++ Programming
    Replies: 14
    Last Post: 11-27-2008, 04:55 PM
  3. Need comment on Factory Pattern Example
    By Bargi in forum C++ Programming
    Replies: 2
    Last Post: 07-04-2008, 05:46 AM
  4. static member initialisation with factory pattern
    By Bench82 in forum C++ Programming
    Replies: 3
    Last Post: 09-01-2006, 04:42 PM
  5. Abstract Factory pattern
    By Just in forum C++ Programming
    Replies: 3
    Last Post: 02-18-2005, 10:58 AM

Tags for this Thread