factory design 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?

    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; 
    }
    

  • Mod

    @tampere2021 sagte in factory design pattern using static:

    static member function doesn't need to be called from an instance of their own class

    This.

    Think about it:

    static member data can be shared across all instances

    There is no static data member in your code.

    static data at function scope persists between function calls

    There is no static variable in your code.

    is it because static inheritance allows objects to be instantiated

    Static inheritance? There is no such thing. Where did you even pick that up?

    Also, I should say, that the factory pattern in general does not rely on static members. You chose to do so, and badly. This is one possible way, but also one of the more unusual ones.



  • @SeppJ sagte in factory design pattern using static:

    static member data can be shared across all instances

    I guess its because static member data can be shared across all instances. please correct me if am wrong.



  • Try to see it from a logical standpoint. Can a factory produce itself or does it produce stuff different from itself? Or the other way around, does a creator produce itself? The creator or factory is an external entity. In C it would be a creator-function, but in C++ you have access levels in objects you have to take into account. If you pick the creator function in C++, you have to make that function be able to access all class members (more precise, be able to call their constructors). You can do this by using friend declarations or you use a static member function. The nice thing about this is, you also get a logical grouping, which helps your design. Using a static member function is actually more convenient compared to a creator function. Though, there are actually more ways to do this, for example static inheritance (policy design pattern).



  • @VLSI_Akiko do you mean to say that the reason for using factory design using static keyword is because static member function doesn't need to be called from an instance of their own class?



  • @tampere2021 sagte in factory design pattern using static:

    @VLSI_Akiko do you mean to say that the reason for using factory design using static keyword is because static member function doesn't need to be called from an instance of their own class?

    Exactly, a static member function is actually a common free-standing function, but with implicit friend declaration (not technically correct, but basically nails it 😁 ).



  • @VLSI_Akiko Ok thanks for clarifying me . I am clear now with usage of static keyword in factory design.



  • @tampere2021
    The next thread where you give a list of options and try to make people pick one.
    Say what you want, but I'm practically convinced that this is for some kind of assignment/test.



  • @hustbaer sagte in factory design pattern using static:

    @tampere2021
    The next thread where you give a list of options and try to make people pick one.
    Say what you want, but I'm practically convinced that this is for some kind of assignment/test.

    I actually got the impression I was talking to a GPT-3 AI based bot. Some of the posts gave me an uncanny valley effect, which is a really odd.


Log in to reply