我现在正在寻找的是一组派生自公共基类的类。 大多数(但不是全部)类需要一些通过模态对话框获得的输入参数。 这些对话框在类的构造函数中设置和执行。 只要对话框未完成,对象就不会完全构造。 延迟构造函数的执行可能会出现什么问题?
我正在考虑使用回调机制替换所有内容,该回调机制提供给对话框以设置对象或使用工厂在构造后立即获取可用对象。 还有哪些其他模式可以解决这种情况?
What I'm looking right now is a set of classes derived from a common base class. Most, but not all, of the classes require some input parameters which are obtained through modal dialogs. Those dialogs are set up and executed in the constructor of the classes. As long as the dialog isn't finished, the object isn't constructed completely. What problems could arise by delaying the execution of a constructor?
I was thinking of replacing everything with a callback mechanism that is provided to the dialogs to set up the objects or using a factory to get usable objects right after construction. What other patterns are there to solve this situation?
最满意答案
就语言而言,不会出现“问题”。 构造函数可以随意使用。
它可能是一个问题,可能会造成混乱。 使用该类的程序员是否会意识到构造函数会长时间阻塞该线程?
在不知道代码的任何细节的情况下,回调或其他异步机制可能会更好,以避免阻塞线程。
No "problems" can arise as far as the language is concerned. The constructor is allowed to take as long as it likes.
Where it might be a problem is in the confusion it might cause. Will the programmer using the class be aware that the constructor blocks the thread for a long time?
Without knowing any details of your code, a callback or some other asynchronous mechanism might be better, to avoid blocking the thread.
更多推荐
发布评论