“终结器守护者”如何实现?在java中工作?
“终结器守护者”[Effective Java,第 30 页] 如何工作?
你用过它们吗?它解决了任何具体问题吗?
How does a "finalizer guardian" [Effective Java , page 30] work ?
Have you used them? Did it solve any specific problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了子类忘记调用超类的
finalize
方法的问题。此模式的工作原理是将一个带有重写的finalize
的额外实例附加到您的超类。这样,如果超类超出范围,附加的实例也会超出范围,这将触发其finalize
的执行,进而调用finalize<封闭类的 /code> 。
下面是一个简短的片段,展示了守护者模式的实际应用:
It solves the problem of the sub-class forgetting to call the
finalize
method of the super-class. This pattern works by attaching an extra instance with overriddenfinalize
to your super-class. This way, if the super-class goes out of scope, the attached instance would also go out of scope, which would trigger the execution of itsfinalize
, which would in turn call thefinalize
of the enclosing class.Here is a short snippet that showcases the guardian pattern in action: