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 t开发者_StackOverflowhe 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
- It would be easy to read widgets from file or some other external source and create them by their names.
Cons
- It would be easy to pass incorrect type name into Widget::create and you'll get error in run-time only.
- You will have no access to widget-specific API without downcasting - i.e. one more source of run-time errors.
- It might be not that easy to find all places where you create a widget of particular type.
Why not get best of both worlds by allowing widget creation both with regular constructors and with a factory?
I think this is not very intuitive. I would rather allow direct accesss to the classes. The question here is, why you want to have it this way. Is there any technical reason? Do you have a higher abstraction layer above the "real classes" that contain the widgets? For example, you have pure virtual classes for the outside API and then an implementation-class? But it is difficult to say if this is any good without knowing how your library is set up, but I am not a big fan of factories myself, unless you really need them. If you have this higher abstraction layer and want that factory, take a look at irrlicht. It uses pure virtual classes for the API and then "implementation classes". The factory chooses the correct implementation depending on the chosen renderer (directx, opengl, software). But they don't identify the wanted class by a string. That's something I really wouldn't do in C++. Just my 2 cents.
I would advise against using a string
as a key, and promote a real method for each subtype.
class Factory
{
public:
std::unique_ptr<Label> createLabel(std::string const& v);
std::unique_ptr<Button> createButton(std::string const& v, Action const* a);
};
This has several advantages:
- Compiler-checked, you could mispell the string, but you cannot get a running program with a mispelled method
- More precise return type, and thus, no need for up-casting (and all its woes)
- More precise parameters, for not all elements require the same set of parameters
Generally, this is done to allow customization. If Factory
is an abstract interface, then you can have several implementations (Windows
, GTK
, Mac
) that create them differently under the hood.
Then, instead of setting up one creator function per type, you would switch the whole factory at once.
Note: performance-wise, virtual functions are much faster than map-lookup by string key
精彩评论