VB6是否可以实现单例设计模式?
在VB6中是否可以实现单例设计模式?
目前,我工作的遗留系统有大量 IO 由特定类的多个实例执行。最好清理所有这些实例并仅由一个实例执行 IO。这将使我们能够向 IO 例程添加有意义的日志记录和监视。
Within VB6 is it possible to implement the Singleton design pattern?
Currently the legacy system I work on has a large about of IO performed by multiple instances of a particular class. It is desirable to clean up all these instances and have the IO performed by one instance only. This would allow us to add meaningful logging and monitoring to the IO routines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以做到这一点,这取决于这是一个具有不同 dll 的多项目应用程序还是单个项目。
如果它是单个项目,并且有大量代码需要您担心更改/破坏,那么我建议如下:
这样做的优点是您不必更改正在使用的许多位置中的 coe clsIOProvider 类。他们很高兴没有意识到该对象现在实际上是一个单例。
如果从头开始编写一个项目,我会采取一些不同的做法,但作为我概述的重构方法应该可以很好地工作。
There are so many ways to do this, and it depends if this is a multi-project application with different dlls or a single project.
If it is single project and there is a large amount of code that you are worrying about chaning/breaking then I suggest the following:
The advantage of this is that you do not have to change the coe in the scores of places that are utilizeing the clsIOProvider class. They are happily unaware that the object is now effectively a singleton.
If coding a proejct from scratch I would do this a bit differently, but as a refactoring appraoch wahat I've outlined should work well.
仅创建和使用一个对象的一个实例是很容易的。如何执行此操作取决于您的代码的作用以及从何处调用它。
在一个进程中,您可以在带有实例的全局模块中使用单个变量,也许可以使用在第一次使用时创建它的工厂函数。
如果它由多个进程共享,则会使事情变得复杂,但可以使用 ActiveX EXE 和运行对象表来完成。
It's easy enough to only create and use one instance of an object. How you do it depends on your code what it does and where it's called from.
In one process, you can just have a single variable in a global module with an instance, maybe with a factory function that creates it on first use.
If it's shared by multiple process, it complicates things, but can be done with an ActiveX EXE and the Running Object Table.