从构造函数调用timeconsuming函数(Calling timeconsuming functions from a constructor)

编程入门 行业动态 更新时间:2024-10-18 19:29:30
从构造函数调用timeconsuming函数(Calling timeconsuming functions from a constructor)

我现在正在寻找的是一组派生自公共基类的类。 大多数(但不是全部)类需要一些通过模态对话框获得的输入参数。 这些对话框在类的构造函数中设置和执行。 只要对话框未完成,对象就不会完全构造。 延迟构造函数的执行可能会出现什么问题?

我正在考虑使用回调机制替换所有内容,该回调机制提供给对话框以设置对象或使用工厂在构造后立即获取可用对象。 还有哪些其他模式可以解决这种情况?

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.

更多推荐

本文发布于:2023-08-07 15:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   timeconsuming   Calling   functions   constructor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!