C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式) 笔记

博客展示了多线程Singleton模式的实现代码。通过定义私有构造器防止外部创建实例,使用静态volatile变量和锁机制确保线程安全,实现单例模式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(很喜欢李建忠老师的这个讲座,可惜暂时没更多的了,继续关注 MSDN WebCast 网络广播

C#面向对象设计模式纵横谈(2):Singleton 单件(创建型模式)



模式分类
模式分类.JPG
 

 

目的

创建型

结构型

行为型

 

 

 

 

 

 

 

工厂方法(Factory Method

适配器(类,Adapter

解释器(Interpreter

模板方法(Template Method

 

 

 

 

抽象工厂(Abstract Factory

生成器(Builder

原型(Prototype

单件(Singleton

适配器(对象,Adapter

桥接(Bridge

组成(Composite

装饰(Decorator

外观(Facade

享元(Flyweight

代理(Proxy

职责链(Chain of Responsibility

  令(Command

迭代器(Iterator

中介者(Mediator

备忘录(Memento

观察者(Observer

  态(State

  略(Strategy

访问者(Visitor



动机(Motivation)
动机Motivation.JPG

意图(Intent)
意图Intent.JPG

Singleton 模式实现

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 单线程 Singleton 模式实现
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
private static Singleton instance;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Singleton() dot.gif{}//私有的构造器使得外部无法创建该类的实例
InBlock.gif
InBlock.gif    
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( instance == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                instance 
= new Singletion();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return instance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif
/// 多线程 Singleton 模式实现
ExpandedBlockEnd.gif
/// </summary>
None.gifpublic class Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
private static volatile Singleton instance = null;
InBlock.gif
InBlock.gif    
private static object lockHelper = new Object();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Singleton() dot.gif{}  //私有的构造器使得外部无法创建该类的实例


InBlock.gif
InBlock.gif    
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
get
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( instance == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
lock ( lockHelper )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if ( instance == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        instance 
= new Singletion();
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return instance;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}


ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 多线程 Singleton 模式实现
ExpandedBlockEnd.gif
/// </summary>
None.gif class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static readonly Singletion Instance = new Singleton();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Singleton() dot.gif{}
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ //// 等同于:
InBlock.gif
InBlock.gif
/// <summary>
InBlock.gif
/// 多线程 Singleton 模式实现
ExpandedBlockEnd.gif
/// </summary>

None.gif class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static readonly Singletion Instance;
InBlock.gif
InBlock.gif    
static Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Instance 
= new Singleton();
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
private Singleton() dot.gif{ }
ExpandedBlockEnd.gif}

 
ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 单线程 Singleton 模式实现
InBlock.gif
/// 支持参数化的构造器方式
ExpandedBlockEnd.gif
/// </summary>

None.gif public   class  Singleton
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public static readonly Singletion instance;
InBlock.gif
InBlock.gif    
private Singleton(int x, int y)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
this.x = x;
InBlock.gif        
this.y = y;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public static Singleton GetInstance(int x, int y0
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( instance == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            instance 
= new Singleton(x, y);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            instance.x 
= x;
InBlock.gif            instance.y 
= y;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
return instance;
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
int x;
InBlock.gif    
int y;
ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值