[英]Adding progress bar to calculation
I have a class that does a complex calculation which may take a long time.我有一个 class 进行复杂的计算,这可能需要很长时间。 I'd like to add a progress bar with Abort button.我想添加一个带有中止按钮的进度条。 However, I would like to avoid mixing calculation code with GUI code if possible.但是,我想尽可能避免将计算代码与 GUI 代码混合使用。
Are there any good design patterns for this?有什么好的设计模式吗?
The relevant design pattern is the Observer pattern: your class doing a calculation is the "subject", it maintains a list of Observers, implementing a common Observer interface, and when its state changes (ie the amount of progress has changed), it notifies each observer via an update() call.相关的设计模式是Observer模式:你的class做一个计算就是“主体”,它维护了一个Observer列表,实现了一个通用的Observer接口,当它的state发生变化(即进度量发生变化)时,通知每个观察者通过 update() 调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.