VB6是否可以实现单例设计模式?

发布于 2024-12-02 13:32:00 字数 115 浏览 1 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尐籹人 2024-12-09 13:32:00

有很多方法可以做到这一点,这取决于这是一个具有不同 dll 的多项目应用程序还是单个项目。

如果它是单个项目,并且有大量代码需要您担心更改/破坏,那么我建议如下:

  1. 给定一个在各处实例化的类 clsIOProvider,在同一项目中创建一个模块 modIOProvider。
  2. 对于 clsIOProvider 中定义的每个方法/属性,在 modIOProvider 中创建相同的方法集。
  3. 这些方法的实现以及类的实例数据应该从 clsIOProvider 克隆到 modIOProvider。
  4. clsIOProvider 中的所有方法和属性都应更改为转发到 modIOProvider 中的实现。该类不应再具有实例数据。
  5. (可选)如果类需要使用构造函数和析构函数(初始化/终止),请将它们也转发给 modIOProvider。在 modIOProvider 中添加单个实例计数器来跟踪实例数量。当实例计数器从 0 到 1 时运行初始化代码,当实例计数器从 1 到 0 时运行终止代码。

这样做的优点是您不必更改正在使用的许多位置中的 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:

  1. Given a class clsIOProvider that is instantiated all over the place, create a module modIOProvider in the same project.
  2. For each method / property defined in clsIOProvider, create the same set of methods in modIOProvider.
  3. The implementation of those methods, as well as the instance data of the class, should be cloned from clsIOProvider to modIOProvider.
  4. All methods and properties in clsIOProvider should be chnaged to forward to the implementation in modIOProvider. The class should no longer have an instance data.
  5. (Optional) If the class requires the use of the constructor and destructor (Initialize/Terminate), forward those to modIOProvider as well. Add a single instnace counter within modIOProvider to track the number of instances. Run your initialzation code when the instance counter goes from 0 to 1, and your termination code when the instance counter goes from 1 to 0.

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.

晚雾 2024-12-09 13:32:00

仅创建和使用一个对象的一个​​实例是很容易的。如何执行此操作取决于您的代码的作用以及从何处调用它。

在一个进程中,您可以在带有实例的全局模块中使用单个变量,也许可以使用在第一次使用时创建它的工厂函数。

如果它由多个进程共享,则会使事情变得复杂,但可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文