I was looking into using the to implement the style and rendering in a little GUI toolkit of mine using the Factory Pattern, and then this crazy Idea struck me. Why not use the Factory Pattern for all widgets?
Right now I’m thinking something in style of:
Widget label = Widget::create("label", "Hello, World");
Widget byebtn = Widget::create("button", "Good Bye!");
byebtn.onEvent("click", &goodByeHandler);
New widget types would be registered with a function like Widget::register(std::string ref, factfunc(*fact))
What would you say is the pros and cons of this method?
I would bet on that there is almost no performance issues using a std::map<std::string> as it is only used when setting things up.
And yes, forgive me for being damaged by JavaScript and any possible syntax errors in my C++ as it was quite a while since I used that language.
Summary of pros and cons
Pros
- It would be easy to load new widgets dynamically from a file
- Widgets could easily be replaced on run-time (it could also assist with unit testing)
- Appeal to Dynamic People
Cons (and possible solutions)
- Not compile-time checked (Could write own build-time check. JS/Python/name it, is not compile-time checked either; and I believe that the developers do quite fine without it)
- Perhaps less performant (Do not need to create Widgets all the time. String Maps would not be used all the time but rather when creating the widget and registering event handlers)
- Up/Downcasting hell (Provide usual class instantiation where needed)
- Possible of making it harder to configure widgets (Make widgets have the same options)
My GUI Toolkit is in its early stages, it lacks quite much on the planning side. And as I’m not used to make GUI frameworks there is probably a lot of questions/design decisions that I have not even though of yet.
Right now I can’t see that widgets would be too different in their set of methods, but that might rather just be because I’ve not gone trough the more advanced widgets in my mind yet.
Thank you all for your input!
Pros
Cons
Why not get best of both worlds by allowing widget creation both with regular constructors and with a factory?