c++ - Is it sane to use the Factory Pattern to instantiate all widgets? -


i looking using implement style , rendering in little gui toolkit of mine using factory pattern, , crazy idea struck me. why not use factory pattern widgets?

right i'm thinking in style of:

widget label = widget::create("label", "hello, world"); widget byebtn = widget::create("button", "good bye!");  byebtn.onevent("click", &goodbyehandler); 

new widget types registered function widget::register(std::string ref, factfunc(*fact))

what pros , cons of method?

i bet on there no performance issues using std::map<std::string> used when setting things up.

and yes, forgive me being damaged javascript , possible syntax errors in c++ quite while since used language.


summary of pros , cons

pros

  • it easy load new widgets dynamically file
  • widgets replaced on run-time (it assist unit testing)
  • appeal dynamic people

cons (and possible solutions)

  • not compile-time checked (could write own build-time check. js/python/name it, not compile-time checked either; , believe developers quite fine without it)
  • perhaps less performant (do not need create widgets time. string maps not used all time rather when creating widget , registering event handlers)
  • up/downcasting hell (provide usual class instantiation needed)
  • possible of making harder configure widgets (make widgets have same options)

my gui toolkit in stages, lacks quite on planning side. , i'm not used make gui frameworks there lot of questions/design decisions have not though of yet.

right can't see widgets different in set of methods, might rather because i've not gone trough more advanced widgets in mind yet.

thank input!

pros

  • it easy read widgets file or other external source , create them names.

cons

  • it easy pass incorrect type name widget::create , you'll error in run-time only.
  • you have no access widget-specific api without downcasting - i.e. 1 more source of run-time errors.
  • it might not easy find places create widget of particular type.

why not best of both worlds allowing widget creation both regular constructors , factory?


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -