介绍
保证一个类仅有一个实例,并提供一个访问它的全局访问点。

示例
保证一个类仅有一个实例。

Singleton
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Singleton
{
/// <summary>
/// 泛型实现单例模式
/// </summary>
/// <typeparam name="T">需要实现单例的类</typeparam>
public class Singleton<T> where T : new()
{
/// <summary>
/// 返回类的实例
/// </summary>
public static T Instance
{
get { return SingletonCreator.instance; }
} class SingletonCreator
{
internal static readonly T instance = new T();
}
}
}

Test

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using Pattern.Singleton; public partial class Singleton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 使用单例模式,保证一个类仅有一个实例
Response.Write(Singleton<Test>.Instance.Time);
Response.Write("<br />");
Response.Write(Singleton<Test>.Instance.Time);
Response.Write("<br />"); // 不用单例模式
Test t = new Test();
Response.Write(t.Time);
Response.Write("<br />");
Test t2 = new Test();
Response.Write(t2.Time);
Response.Write("<br />");
}
} public class Test
{
private DateTime _time; public Test()
{
System.Threading.Thread.Sleep();
_time = DateTime.Now;
} public string Time
{
get { return _time.ToString(); }
}
}

运行结果
2007-2-10 22:35:11
2007-2-10 22:35:11
2007-2-10 22:35:14
2007-2-10 22:35:17

[索引页][源码下载]

***********************************************************************************************
*【Author】:webabcd
*【Date】:2013年08月18日
*【URL】:http://www.cnblogs.com/webabcd/archive/2007/04/01/696021.html
*【Notice】:
*1、本文为原创技术文章,首发博客园个人站点。
*2、请尊重原创的成果,转载和引用请注明作者及出处。
*3、本文必须全文转载和引用,任何组织和个人未授权不能修改任何内容,并且未授权不可用于商业。
*4、本声明为文章一部分,转载和引用必须包括在原文中。
***********************************************************************************************

乐在其中设计模式(C#) - 单例模式(Singleton Pattern)【转】的更多相关文章

  1. 乐在其中设计模式(C#) - 单例模式(Singleton Pattern)

    原文:乐在其中设计模式(C#) - 单例模式(Singleton Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 单例模式(Singleton Pattern) 作者:weba ...

  2. 设计模式之单例模式(Singleton Pattern)

    单例模式 单例模式(Singleton Pattern)在java中算是最常用的设计模式之一,主要用于控制控制类实例的数量,防止外部实例化或者修改.单例模式在某些场景下可以提高系统运行效率.实现中的主 ...

  3. 【设计模式】单例模式 Singleton Pattern

    通常我们在写程序的时候会碰到一个类只允许在整个系统中只存在一个实例(Instance)  的情况, 比如说我们想做一计数器,统计某些接口调用的次数,通常我们的数据库连接也是只期望有一个实例.Windo ...

  4. 二十四种设计模式:单例模式(Singleton Pattern)

    单例模式(Singleton Pattern) 介绍保证一个类仅有一个实例,并提供一个访问它的全局访问点. 示例保证一个类仅有一个实例. Singleton using System; using S ...

  5. Java 设计模式(三)-单例模式(Singleton Pattern)

    1     概念定义 1.1   定义 确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 1.2   类型 创建类模式 1.3   难点 1)多个虚拟机 当系统中的单例类被拷贝运行在多 ...

  6. python 设计模式之单例模式 Singleton Pattern

    #引入 一个类被设计出来,就意味着它具有某种行为(方法),属性(成员变量).一般情况下,当我们想使用这个类时,会使用new 关键字,这时候jvm会帮我们构造一个该类的实例.这么做会比较耗费资源. 如果 ...

  7. 【UE4 设计模式】单例模式 Singleton Pattern

    概述 描述 保证一个类只有一个实例 提供一个访问该实例的全局节点,可以视为一个全局变量 仅在首次请求单例对象时对其进行初始化. 套路 将默认构造函数设为私有, 防止其他对象使用单例类的 new运算符. ...

  8. 浅谈设计模式--单例模式(Singleton Pattern)

    题外话:好久没写blog,做知识归纳整理了.本来设计模式就是个坑,各种文章也写烂了.不过,不是自己写的东西,缺少点知识的存在感.目前还没做到光看即能记住,得写.所以准备跳入设计模式这个大坑. 开篇先贡 ...

  9. 设计模式系列之单例模式(Singleton Pattern)——确保对象的唯一性

    模式概述 模式定义 模式结构图 饿汉式单例与懒汉式单例 饿汉式单例 懒汉式单例 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 适用场景 说明:设计模式系列文章是读刘伟所著 ...

随机推荐

  1. mysqldump造成Buffer Pool污染的研究

    前言: 最近Oracle MySQL在其官方Blog上贴出了 5.6中一些变量默认值的修改.其中innodb_old_blocks_time 的默认值从0替换成了1000(即1s) 关于该参数的作用摘 ...

  2. 【转】从零开始,让你的框架支持CocoaPods

    首先概括一个大概的步骤: 代码上传到Github 创建podspec文件 在Github上创建release版本 注册CocoaPods账号 上传代码到CocoaPods 检验是否上传成功 更新框架版 ...

  3. ocp 1Z0-051 141-175题解析

    141. View the Exhibitand examine the structure of CUSTOMERS and GRADES tables. You need to displayna ...

  4. Gradle – Spring 4 MVC Hello World Example

    In this tutorial, we will show you a Gradle + Spring 4 MVC, Hello World Example (JSP view), XML conf ...

  5. MongoDB的安装配置

    1,下载: http://www.mongodb.org/downloads 2.4.5版:http://www.mongodb.org/dr/fastdl.mongodb.org/linux/mon ...

  6. VMware搭建12.0搭建Mac OS10.11详细过程

    1.软件准备 1.1VMware12.0 1.2VMware增强包 1.3Mac OS10.11 cdr(相当于dmg) 1.4securable.exe 2.软件破解 2.1VMware输入序列号破 ...

  7. 绑定线程到特定CPU处理器

    参考这篇文章 http://blog.chinaunix.net/uid-27761170-id-5050258.html 代码如下: #define _GNU_SOURCE #include < ...

  8. oracle 中控制文件中到底记录了哪些信息

     oracle 控制文件中的信息  oracle 11g                                             oracle 10g   DATABASE       ...

  9. IOC使用Unity 实现依赖注入

    转自:http://www.cnblogs.com/techborther/archive/2012/01/06/2313498.html http://www.cnblogs.com/xishuai ...

  10. corpus  academic writing

    http://micusp.elicorpora.info/ http://corpus.byu.edu/coca/ http://rcpce.engl.polyu.edu.hk/RACorpus/