惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
博客园 - 司徒正美
美团技术团队
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Troy Hunt's Blog
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
P
Proofpoint News Feed
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
B
Blog
NISL@THU
NISL@THU
月光博客
月光博客
博客园 - 【当耐特】
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
腾讯CDC
L
Lohrmann on Cybersecurity
The Cloudflare Blog
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
S
Secure Thoughts
Spread Privacy
Spread Privacy
有赞技术团队
有赞技术团队
The Last Watchdog
The Last Watchdog
Project Zero
Project Zero
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Vercel News
Vercel News
H
Hacker News: Front Page
S
SegmentFault 最新的问题
Schneier on Security
Schneier on Security
aimingoo的专栏
aimingoo的专栏
P
Privacy & Cybersecurity Law Blog
博客园 - 三生石上(FineUI控件)
Forbes - Security
Forbes - Security
C
CXSECURITY Database RSS Feed - CXSecurity.com
I
InfoQ
T
Tailwind CSS Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
W
WeLiveSecurity
小众软件
小众软件
Recorded Future
Recorded Future
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - miniflyfish

修改导入的博客图片地址链接 抓取新浪博客中的图片 blogml导入blogengine注意事项 xml repeater dataset资料备忘 将新浪博客导入到blogengine webmatrix、visualstidio2010、blogengine cs2.1的安装备忘 Adapter适配器模式(结构型模式) prototype原型(创建型模式) Factory Method 抽象工厂模式(创建型) builder生成器(创建型模式) Abstract Factory 抽象工厂(创建型模式) 代码存放样例 面向对象设计模式与原则 ASP.NET中的XML ASP.NET的事件处理 ASP.NET WEB服务器控件的使用 AJAX简介与web2.0 web 服务
singleton单件(创建型模式)
miniflyfish · 2006-08-08 · via 博客园 - miniflyfish

模式分类:
从目的来看:
创建型(creational)模式:负责对象创建
结构型(structural)模式:处理类与对象间的组合
行为型(Behavioral)模式:类与对象交互中的职责分配

从范围来看:
类模式处理类与子类的静态关系
对象模式处理对象间的动态关系

单件模式确立的目的是保证类只有一个实例在系统中运行,并提供一个该实例的全局访问点,确保它们的逻辑正确性以及良好的效率
如何确保只有一个实例运行?这是类设计者的责任,而不是使用者的责任

单线程Singleton模式的几个要点:
1.Singleton模式中的实例构造器可以设置为Protected以允许子类派生
2.Singleton模式一般不要支持ICloneable接口,因为这可能导致多个对象的实例,与singleton模式的初衷相违背
3.Singleton模式一般不要支持序列化,因为这也可能导致多个对象实例。序列化可以创建类的实例。
4.singleton模式只考虑到了对象创建的管理,没有考虑对象的销毁。
5.不能应对多线程环境。

单线程singleton模式
public class Singleton
{
    private static Singleton instance;

    private Singleton(){}
   
    public static Singleton Instance
   {
       get
              {
                      if (instance==null)
                          {
                                 instance = new Singleton();
                          }
                        return instance;

               }
    }
}

多线程singleton模式
public class Singleton
{
    private static volatile Singleton instance=null;
  
    private static object lockHelper = new object();

    private Singleton(){}
   
    public static Singleton Instance
   {
       get
              {
                      if (instance==null)
                          {
                              lock (lockHelper)
                                {
                                         if (instance==null)
                                       {
                                                instance = new Singleton();
                                        }
                                  }

                          }
                        return instance;

               }
    }
}

经典的singleton模式
 class Singleton
{
 public static readonly Singleton Instance=new Singleton();
    
    private Singleton(){ }

 以上代码相当于下面的代码

 class Singleton
{
 public static readonly Singleton Instance;
  
    static Singleton()
   {
         Instance=new Singleton();
    }
    
    private Singleton(){ }

其中    static Singleton()
   {
         Instance=new Singleton();
    }是在编译时由系统调用的,在实例化Instance变量之前调用

readonly的作用是防止用户赋予null值

也可以在构造函数中带参数
在单线程模式中,如下
public class Singleton
{
    private static Singleton instance;

    private Singleton(int x ,int y)
    {
        this.x=x;
        this.y=y;
    }
    public static Singleton Instance
   {
       get
              {
                      if (instance==null)
                          {
                                 instance = new Singleton(x,y);
                          }
                        return instance;

               }
    }
   int x;
   int y;
}

如果没有初始化函数,则可以通过将属性剥离的方式实现,具体实现如下:
class Singleton
{
 public static readonly Singleton Instance=new Singleton();
    
    private Singleton(){ }
 
 public int X
   {
       get 
       {
            return this.x;
       }
        set
        {
               this.x=x;
         }
   }

   public int Y
   {
       get 
       {
            return this.y;
       }
        set
        {
               this.y=y;
         }
   }

在调用函数中先创建类的实例,再设置对象属性值。

singleton模式扩展
将一个实例扩展到N个实例,例如对象池的实现
将new构造器的调用转移到其他类中,例如多个类协同工作环境中
理解和扩展singleton模式的和核心是“如何控制用户使用new对一个类的实例构造器的任意调用”