c++ - Is inheritance a good tool to design a builder/factory -
i have several complex classes, constructed using separate creator classes inherits class create.
one example might graph constructed unordered data.
class graph{ //.... public: void showdata(); protected: std::vector<std::pair<int,int> > mconnecteddata; } class graphcreator:private graph{ public: //... void construct(); private: std::map<double,int> msomehelpercontainer; //... } for construction need many helper functions , helper data, put in class graphcreator. since many graph related functions necessary , since need data of graph @ case use private inheritance. since no means famous is-a relation , since private inheritance considered hint bad design have doubts: idea , 1 appropriate way design factory or there major drawbacks have no thought ? better way of designing such factory ?
edit:
thanks answers far ! additional information make reason used approach clearer. cannot use static creation method (far many state variables in creator) , have constraint: want provide graph independent creator (e.g read file method) in library other people. should not have care creator. therefore little unsure friend usage, since adds code inside graph class.
it not way (inheritance data is).
the traditional approach either:
- make
graphcreatorfriendofgraph - implement method
staticbuildmethod ingraph(directly)
the decision depends on whether need "factory" stateful. class used represent state, method cannot do.
- therefore if need state need
classstore it, ,graphcreatorbest bet. - for stateless approach,
staticmethod more lightweight.
if undecided, pick easiest (static method) , see how far goes :)
Comments
Post a Comment