Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】

     

I am here to continue the explanation of Design Patterns. Today we will explain the easiest yet an important design pattern called Singleton.

这里我继续来解释设计模式。今天我将会来解释,最简单但非常重要的设计模式,也就是单例模式。

In case you have not had a look at our first article, go through the following link:

这里假设,你还没有看我的第一篇文章,请先回去阅读吧,下面是链接:

Before talking about its implementation let’s begin with some fundamental questions as in the following.

来讨论单例模式是如何实现之前,我们先看看下面一些基础的问题吧。

Use of the Singleton Pattern【使用单例模式】

As the name suggests, the Singleton Pattern allows only one instance of a class to be created.

就像这个名字一样,单例模式只允许,创建一个类的实例。

When do we need to have only one instance of a class? 

为什么我们只需要一个类的实例?

There are many possible requiremetns for a instance of a class but they all tend to have the one objective that we don’t want to change the state of the object or we want to keep the class stateless.

这里有许多可能的requiremetns类的实例,但是它们都想要只有一个对象,所以我们不能去改变对象的状态或者使对象的状态变成无效的。

A simple example could be that you want to load some master data at once and let the consumers of the data make a call to the Singleton class instead of each consumer making various calls by creating a new instance.

举一个简单的例子,你想要立刻加载主表的数据,并且让一个单例类来调用获取客户表的数据,而不是对于每一个客户,都来创建一个类的实例来调用获取数据。

In general, in any complex enterprise application, Repository and Data Access Layer classes can be seen as a Singleton since typically we don’t want them to maintain the state in these layers.

一般来说,在任何复杂点的企业级应用程序中,仓储和数据访问层的类,可以作为单例来看待,因为我们不想要它们在这些层中,保持状态。

Some other example could be cross-cutting concerns like Logging, Configuration, Caching and so forth that can also be implemented as a Singleton since we want a single and global point of access to these classes.
其他的例子就是横切关注点了,例如日志,系统配置,缓存等等,可以同样设计为单例,因为我们想要对这些类,进行全局的,单一的访问。

Apart from the core consideration explained above, I have seen that developers, mostly not so experienced sometimes, create unnecessarily instances that creates not just an overhead to memory but also impacts the overall performance of an application.

除了上面解释的,我看到过很多的开发者,有时候并不是那么有经验,他们创建不必要的实例,这不仅仅增加了内存的开销,同样也影响了系统的性能。

Why not Static classes【为什么不使用静态类】

There can be several reasons why to not use a static class however I can think of a few as follows.

至于为什么不使用静态类,我认为有如下的原因:

  • There can be cases where you want to implement interfaces (maybe to implement IOC, I will explain IOC later) in a class that can be done in a Singleton implementation but not in the static one.

     可能存在这样的情况:你想要在类中实现某个接口【可能是实现IOC,我后面会降到IOC】,这种情况可以在单例中做到,但是不能在静态类实现。
  • If required, you can use a singleton class as a method parameter whereas you cannot do that with a static class.
         如果有需要,你可以把单例类作为一个方法的参数,但是,你不能对静态类也这样做。

Special care for Singleton classes【特别要说的就是单例类】

We need to take special care for Singleton classes. The idea of a state of a class comes with some extra care that means we need to handle synchronization issues in multi-threaded environments.

我们需要特别说到单例类,类的状态,有一些需要注意点,也就是我们需要在多线程的环境中处理同步的问题。

Enough theory, now let’s talk about implementation.

好了,理论已经足够了,现在我们来讨论怎么实现单例模式吧。

Let’s have a look at the most basic implementation.

我们先看看最基本的实现。

In the first example below, we have implemented a Singleton with Lazy loading since the instance will not be created until the caller calls the GetInstance method for the first time.

在下面的例子中,我实现了一个懒加载的单例,因为这个实例只有在GetInstance方法第一次被调用的时候,才会创建类的实例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
/// <summary>
/// SingletonClass单例模式学习
/// </summary>
public class SingletonClass
{
/// <summary>
/// 创建私有的,静态的,类的变量
/// </summary>
private static SingletonClass instance = null; /// <summary>
/// 创建私有的SingletonClass无参构造函数
/// </summary>
private SingletonClass()
{ } /// <summary>
/// 创建静态的属性GetInstance
/// </summary>
public static SingletonClass GetInstance
{
get
{
if (instance == null)
{
//实例化SingletonClass
instance = new SingletonClass();
}
return instance;
}
}
}
}

Let’s try to fix the sync issue that may arise in multi-threaded environments. For this, we will use a double-lock mechanism.

现在我们来修复,上面例子中在多线程环境中,可能出现的同步问题吧。对于这个,我将会使用一个双锁机制。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
/// <summary>
/// SingletonClass单例模式学习
/// </summary>
public class SingletonClass
{
/// <summary>
/// 创建私有的,静态的,类的变量
/// </summary>
private static SingletonClass instance = null; private static object lockMe = new object();
/// <summary>
/// 创建私有的SingletonClass无参构造函数
/// </summary>
private SingletonClass()
{ } /// <summary>
/// 创建静态的属性GetInstance
/// </summary>
public static SingletonClass GetInstance
{
get
{
if (instance == null)
{
lock (lockMe)
{
if (instance == null)
{
//实例化SingletonClass
instance = new SingletonClass();
}
} }
return instance;
}
}
}
}

And in the last, Singleton with static initializations. Please note that the .NET Framework guarantees thread safety for static initialization so we don’t need extra care for sync issues however we may not get the benefit of lazy loading of objects here.

最后,我们看下单例模式静态的初始化。请注意对于静态的初始化,.NET Framework保证了线程安全,我们不必要去关心同步的问题,但是这种情况下,我们不能从懒加载对象中获益。

public class SingletonClass
{
private static SingletonClass instance = new SingletonClass();
private SingletonClass() {}
public static SingletonClass GetInstance
{
get
{
return instance;
}
}
}

I hope you have liked this article. I look forward to your comments/suggestions.

我希望你喜欢,这篇文章,期待你的评论和建议。

Design Patterns Simplified - Part 2 (Singleton)【设计模式简述--第二部分(单例模式)】的更多相关文章

  1. 【翻译】设计模式学习系列1---【Design Patterns Simplified: Part 1【设计模式简述:第一部分】】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part1/ Design Pattern ...

  2. Design Patterns Simplified - Part 3 (Simple Factory)【设计模式简述--第三部分(简单工厂)】

    原文链接:http://www.c-sharpcorner.com/UploadFile/19b1bd/design-patterns-simplified-part3-factory/ Design ...

  3. 设计模式(Design Patterns)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  4. 设计模式(Design Patterns)Java版

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...

  5. Java设计模式(Design Patterns)——可复用面向对象软件的基础

    设计模式(Design Patterns) 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结. 使用设计模式是为了可重用代码.让代码更容易被他 ...

  6. 图书-软件架构:《Design Patterns: Elements of Reusable Object-Oriented Software》(即后述《设计模式》一书)

    ylbtech-图书-软件架构:<Design Patterns: Elements of Reusable Object-Oriented Software>(即后述<设计模式&g ...

  7. Learning JavaScript Design Patterns The Singleton Pattern

    The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...

  8. Design Patterns in Smalltalk MVC 在Smalltalk的MVC设计模式

    Design Patterns in Smalltalk    MVC在Smalltalk的MVC设计模式 The Model/View/Controller (MVC) triad ofclasse ...

  9. 云设计模式-Design patterns for microservices

    云设计模式 https://azure.microsoft.com/zh-cn/blog/design-patterns-for-microservices/ https://www.cnblogs. ...

随机推荐

  1. window.onload与$(document).ready()的区别

    对于很多初学者来说,window.onload出现在代码中的频率非常高,这似乎变成了一种习惯,可是并不知道具体为什么要加这句代码,可以做几个试验对比: 实验一: <script> docu ...

  2. .Net客户端监听ZooKeeper节点数据变化

    一个很简单的例子,用途是监听zookeeper中某个节点数据的变化,具体请参见代码中的注释 using System; using System.Collections.Generic; using ...

  3. ADO.NET Entity Framework CodeFirst 如何输出日志(EF 5.0)

    ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用的EFProviderWrappers ,这个组件好久没有更新了,对于SQL执行日志的解决方案的需求 ...

  4. Expert 诊断优化系列------------------你的CPU高么?

    现在很多用户被数据库的慢的问题所困扰,又苦于花钱请一个专业的DBA成本太高.软件维护人员对数据库的了解又不是那么深入,所以导致问题迟迟不能解决,或只能暂时解决不能得到根治.开发人员解决数据问题基本又是 ...

  5. Spring Rabbitmq HelloWorld实例

    之前的博客和大家分享了Rabbitmq的基本框架,及其工作原理,网址为 < http://www.cnblogs.com/jun-ma/p/4840869.html >.今天呢,想和大家一 ...

  6. Net作业调度(三) — Quartz.Net进阶

    介绍 前面介绍Quartz.Net的基本用法,但在实际应用中,往往有更多的特性需求,比如记录job执行的执行历史,发邮件等. 阅读目录 Quartz.Net插件 TriggerListener,Job ...

  7. [.net 面向对象程序设计进阶] (23) 团队开发利器(二)优秀的版本控制工具SVN(上)

    [.net 面向对象程序设计进阶] (23) 团队开发利器(二)优秀的版本控制工具SVN(上) 本篇导读: 上篇介绍了常用的代码管理工具VSS,看了一下评论,很多同学深恶痛绝,有的甚至因为公司使用VS ...

  8. 高灵活度,高适用性,高性能,轻量级的 ORM 实现

    ORM(Object-Relational Mapping 对象关系映射),它的作用是在关系型数据库和业务实体对象之间作一个映射,目的是提供易于理解的模型化数据的方法. ORM虽然有诸多好处,但是在实 ...

  9. Oracle:从SQL文件批量导入数据

    进入DOS界面. 进入SQL文件目录. 在命令提示下运行SqlPlus,c:\sql>sqlplus user_name/password@net_service_name 指定SQL执行日志文 ...

  10. Java邮件发送与接收原理

    一. 邮件开发涉及到的一些基本概念 1.1.邮件服务器和电子邮箱 要在Internet上提供电子邮件功能,必须有专门的电子邮件服务器.例如现在Internet很多提供邮件服务的厂商:sina.sohu ...