如何调试一个广泛使用观察者模式的系统?
在尝试更好地理解观察者模式时,它有哪些属性?在主要使用观察者方法的系统中,人们将如何在这样的系统中调试大段代码?
In trying to better understand the Observer pattern, what are some of its properties and in a system that predominately uses the Observer method how would one go about debugging large sections of code in such a system?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用观察者模式,主体(或辅助类)通常有一个观察者列表。当事件触发时,它会循环遍历列表并调用这些观察者中的方法来告诉它们有关该事件的信息。
所以这是完全可以调试的。如果这些观察者也是程序中的类,则您只需遵循循环并单步执行每个事件处理程序即可。
如果您的观察者是外部对象(COM)或预编译对象(obj 文件),则您不能这样做,但也没有其他方法来检查该代码,因此这个“问题”是一个更普遍的问题,并且不特定于观察者模式。
Using the observer pattern, the subject (or a helper class) usually has a list of observers. When an event fires, it loops through the list and calls a method in those observers to tell them about the event.
So this is perfectly debuggable. If these observers are classes in your program too, you can just follow the loop and step into each of the event handlers.
If you observer is an external object (COM) or a precombiled object (obj file), you can't do that, but there are not other ways to inspect that code as well, so this 'problem' is a more general problem and not specific to the observer pattern.