c#.net应用程序中的单例模式 [英] Singleton pattern in c#.net application

查看:108
本文介绍了c#.net应用程序中的单例模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是设计模式的新手。我想在我的c#.net应用程序中实现Singleton设计模式。



我的应用程序中有几个课程说

Class1,Class2和3级



在某些页面中我会打电话给这个班级中的任何一个,其他我可以打电话给所有三个班级。



如何在我的应用程序中实现单例模式。



如果您可以提供少量样本,那就太棒了。



提前谢谢。

Hi All,

I am new to designing patterns. I would like to implement Singleton design pattern in my c#.net application.

I have few classes in my application say
Class1, Class2 and Class3

In some pages i call any one of this class, in other i may call all the three classes.

How do i implement singleton pattern in my application.

It would be great if you can provide few samples .

Thanks in advance.

推荐答案

您可以创建一个管理器类来获取这些类的实例。您只需创建一次对象,当您需要此类时,您不会创建对象并从manager类中获取这些实例。我没有选择或欣赏的另一个解决方案是在类中创建静态公共实例,并且无需任何调用即可使用这些实例。



这是第一个解决方案的好例子:



http://msdn.microsoft.com/en-us/library/ff650316.aspx [ ^ ]
You can create a manager class to get instances of these classes. You just once create objects and when you need this class you do not create objects and get these instances from manager class. Another solution which I do not choose or appreciate is create a static public instances in a class and just use these without any call.

This is a good example for first solution:

http://msdn.microsoft.com/en-us/library/ff650316.aspx[^]


如果你google或bing对于单身模式c#,你将得到超过120k的建议。或者尝试使用此代码项目屏幕右上角的搜索文章,问题和提示,从此网站获取47页建议
If you google or bing for singleton pattern c# you will get over 120k suggestions. Or try using the Search for Articles, questions, tips in the top right hand corner of this codeproject screen to get 47 pages of suggestions from this very site


这篇关于c#.net应用程序中的单例模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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