I have the following problem.
I got a class PluginLoader which oversees loading of plugins. It divides sub-stages of work to other classes like Plugin. Plugin calls functions of PluginLoader in its processing. Let's call that function AddData. Here, PluginLoader has to check if the data it receives is duplicate. For that, it uses a ConflictResolver class. Now, my problem is how to make an object of ConflictResolver available to PluginLoader. There are 3 ways I see out of this.
- Use a
ConflictResolverFactoryclass and create an object ofConflictResolverforPluginLoader. - Pass a constructed
ConflictResolver*to thePluginLoadervia its constructor or a member functionSetConflictResolverand store it in a member variable and use it later. Both ways have drawbacks. If I pass it in the constructor, I will have to throw if the pointer isNULL. And I can't use exceptions as it is the custom here. If I pass it viaSetConflictResolver, there is no way that I can guarantee that that function will be actually called by the user. Or I will have to che开发者_如何学JAVAck whether the memberConflictResolver*isNULLeverywhere I use it. - Pass a
ConflictResolver &toPluginLoadersLoadmethod where all the work will be done. In turn,Plugins Load method has to accept aConflictResolver &as well (though it has no use for it) and pass that back toAddDatawherePluginLoaderwill be able to use it.
Third method is safer compared to second. However, I have to pass around a reference even when it is not used.
If the first method cannot be used, what is the best way to do this?
Apologies for the wall :wq!
You could pass a ConflictResolver& to the PluginLoader constructor. You can now guarantee that the object is not null.
加载中,请稍侯......
精彩评论