HeadFirst DesignPattern读书摘记(4)--单件模式

单件模式定义:取保一个类中只有一个实例,并提供一个全局访问点。
下面是实现单件模式的经典做法:
using System;
using System.Collections.Generic;
using System.Text;

namespace 设计模式
{
    
public class Singleton
    
{
        
//用来记录单件类中的唯一实例
        private static Singleton _uniqueInstance;

        
//是由构造器,只有自己才能够访问自己
        private Singleton()
        
{

        }

        
//公开一个全局访问点,来得到这个类的实例
        public static Singleton GetInstance()
        
{
            
//什么时候使用这个类,再进行实例化得到这个类的一个实例
            if (_uniqueInstanc == null)
            
{
                _uniqueInstance 
= new Singleton();
            }

            
return _uniqueInstance;
        }

        
//这个类的其他方法
    }



}

posted @ 2008-03-17 14:29  collum  阅读(119)  评论(0)    收藏  举报