c++ - Giant switch statement for constructors -
c++ - Giant switch statement for constructors -
i have container holds bunch of pointers base of operations class, , function takes input , returns class subclass of base of operations class. subclass returns depends on input.
right now, have giant switch statement this:
class base of operations { ... } class : public base of operations { ... } class b : public base of operations { ... } ... class z : public base of operations { ... } base* depends(int input) { switch (input) { case 1: homecoming new a(...); case 2: homecoming new b(...); ... case 26: homecoming new z(...); default: ... } }
i wondering if there's improve way design this. don't know many "design patterns" (i think that's they're called) don't know if there's (obvious) improve way design this.
what looking factory method pattern.
the of import thing here remove need base of operations class have knowledge of derived class implementations. bad design base of operations class have knowledge derived classes.
factory method pattern addresses above problem creation occurs outside of base of operations class.
c++ inheritance switch-statement class-hierarchy construction
Comments
Post a Comment