我们如何以及在哪种情况下使用Singletone设计模式? [英] how and in which situation we can use Singletone design pattern?

查看:68
本文介绍了我们如何以及在哪种情况下使用Singletone设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何以及在哪种情况下使用 Singletone 设计模式?

Singletone设计模式的优势是什么?

how and in which situation we can use Singletone design pattern?
what is the advantages of Singletone design pattern?

推荐答案

实现单线程模式线程安全是非常重要的。对于它的实现,应该是我的单线类是线程安全的。您可以使用readonly修饰符实现这一点。



To implement singletone pattern thread safe is a very important. To its implementation, It should be makesure that my singletone class is thread safe. You can achieve that using readonly modifier.

public class MySingleToneClass
{
   private static readonly MySingleToneClass _instance = new MySingleToneClass();

   private MySingleToneClass(){}

   public static MySingleToneClass GetInstance()
   {
      return _instance;
   }
}



关于SingleTone模式的一个重要注意事项是许多专家都认为SingleTone是反模式的。 SingleTone类背后的原因不是单元可测试,也不鼓励全局对象。与全局数据一样,全局对象会产生许多问题。


One important note regarding SingleTone pattern that is many experts are aggreed that SingleTone is anti-pattern. The reason behind that SingleTone class is not properly unit-testable and also they discourage global object. Like global data, global object create many problems.


此链接演示了Singleton模式的真实示例

http://www.dofactory.com/Patterns/PatternSingleton.aspx [ ^ ]

http://softwarekaffee.blogspot.in/2012/02/real-world-example-of-singleton-design.html [ ^ ]



希望这有帮助
This links demonstrates a real world example of Singleton pattern
http://www.dofactory.com/Patterns/PatternSingleton.aspx[^]
http://softwarekaffee.blogspot.in/2012/02/real-world-example-of-singleton-design.html[^]

Hope this helps


目的:

Singletone通常用于对象优化目的。开发软件的项目,其中对象的数量是一个重要问题,软件重量是我们使用单音的重要问题。

如何:

第1步:创建课程

示例:

Purpose :
Singletone is generally use for object optimization purpose. A project to develop a software where number of object is an important issue and the software weight is an important issue we use Single tone.
How :
Step 1: Create a class
Example:
class SingletoneExample
{
}



第2步:将构造函数设置为私有。

示例:


Step 2: Set the constructor as private.
Example:

class SingletoneExample
{
   private SingletoneExample()
  {
  }
}



步骤3:创建类类型引用变量。

示例:


Step 3: Create class type reference variable.
Example :

class SingletoneExample
{
   private SingletoneExample instance;
   private SingletoneExample()
  {
  }
}





第4步:创建方法。将方法类型设置为Class Type。使用类类型varible的引用创建一个对象并将其返回。



Step 4: Create a method. Set the method type as Class Type.create a object with the reference of class type varible and return it.

class SingletoneExample
{
   private SingletoneExample instance;
   private SingletoneExample()
  {
  }
  public static SingletoneExample getInstance()
  {
    instance=new SingletoneExample();
    return instance;
  }
}



第5步:从其他课程中调用singletoneExample类行为。

第6步:单线人例子课程成为单身人士。


Step 5: From other class you call the singletoneExample class behaviour.
Step 6: The singletoneExample class becomes singletone.


这篇关于我们如何以及在哪种情况下使用Singletone设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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