在多个Windows窗体中使用Singleton Pattern时的重复代码 [英] Duplicated Code when use Singleton Pattern in more than one Windows Form

查看:76
本文介绍了在多个Windows窗体中使用Singleton Pattern时的重复代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:



我需要创建十个不同的Windows窗体,所以我必须使用十次单独模式。我认为它太复杂了。我们可以找到一种方法来减少重复的代码。我打算使用继承,但构造函数在基类中是私有的。



我的英语不好,也许问题是不清楚,谢谢你阅读!

Example:

I need to create ten different Windows Forms,so I have to use the Singleton Pattern ten times.I think it is too complex.Can we find a way to reduce duplicated code.I plan to use inheritance,but the constructor is private in the base class.

My English is not good and maybe the question is not clear,Thank you for reading!

推荐答案

Singleton模式的整个想法是你只创建一个Singleton对象的实例。如果你要创建一个Singleton表单的十个实例,那么你肯定做错了......这可以解释复杂性!
The whole idea of the Singleton Pattern is that you only ever create one instance of the Singleton object. If you are creating ten instances of a Singleton Form, then you are very definitely doing it wrong...which would explain the complexity!


创建一个泛型类。我的基础太糟糕了。
create a generic class.My foundation is too bad.


如何在主窗体中保留这些表单的列表/字典?当所需的表单不在列表中时,创建它并将其添加到列表中,否则从列表中检索它。
What about keeping a List/Dictionary of those forms in the main form? When the required form is not in the list, create it and add it to the list, otherwise retrieve it from the list.


这篇关于在多个Windows窗体中使用Singleton Pattern时的重复代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆