跟java里一样吧!
将构造函数声明为private,在构造一个接口得到。
class SingletonClass
{
   private SingletonClass m_singleton = new SingletonClass();
   private SingletonClass()
   {   }
   
   public getSingleton()
   {
      return m_singleton;
   }
}不知道这样对不对了。

解决方案 »

  1.   

    跟java里一样吧!
    将构造函数声明为private,在构造一个接口得到。
    class SingletonClass
    {
       private SingletonClass m_singleton = new SingletonClass();
       private SingletonClass()
       {   }
       
       public getSingleton()
       {
          return m_singleton;
       }
    }不知道这样对不对了。
      

  2.   

    A sample :
    public class A
    {
       private C B;   public static C getObject()
       {
         if(B==null)//B的实例不存在存在,新建一个并返回
         {
              B=new C();
              return B;
          }
          else      //B的实例已经存在,直接返回B
          {
             return B;
          }
         
       }  
    }public class C
    {
      ......
    }
      

  3.   

    小玩艺见笑了
    using System;namespace JoeM.DesignPatternImplement.Singleton
    {
    /// <summary>
    /// SingleThreadedSingleton
    /// 
    /// Goal: Global access and instantiation control.
    ///   A class that has only one instance and you need
    /// to provide a global point of access to the instance. 
    ///       
    /// Advantage: 
    ///   1. Beause the instance is created in the Instance property 
    /// method, the class can exercise addtional functionality.
    ///   2. The instantiation is not performed until an object asks
    /// for an instance; this approach is referred to as lazy instantiation.
    ///   3. The class ed sealed to prevent derivation, which could 
    /// add instances.
    /// 
    /// Disadvantage: 
    ///   It is not safe for multithreaded environments. If seperate threads
    /// of excution enter the Instance property instance at the same
    /// time, more that one Singleton object may be created.
    /// 
    /// Implement Strategy:
    ///    Use .NET framework static initialization.
    /// </summary>
    public sealed class SingleThreadedSingleton
    {
    private static SingleThreadedSingleton _instance; private int _counter; public static SingleThreadedSingleton Instance
    {
    get
    {
    if (_instance ==null)
    {
    _instance=new SingleThreadedSingleton();
    _instance._counter=100;
    } _instance._counter++;
    return _instance;
    }
    } public int Counter
    {
    get
    {
    return _counter;
    }
    }
    }
    /// <summary>
    /// MultiThreadedSingleton
    /// 
    /// Situation:
    ///    When your application must delay the instantiation, use a
    /// non-default constructor or perform other tasks before the 
    /// instantiation, and work in a multithreaded envirionment.
        ///
        /// Advantage:
        ///    1. Solves the thread concurrency problems while avoiding an
        /// exclusive lock in every call to Instance property method.
        ///    2. Delay the instantiation until the object if first accessed.
        ///       
    /// Implement Strategy:
    ///    Double-Check locking.
    ///    
    /// Implement Details:
    ///    1. The variable is declared to be volatile to ensure
    /// that assignment to the instance variable completes before
    /// the instance variable can be access.  
    ///    2. Use the syncRoot to lock on, rather than locking on the 
    /// the type itself, to avoid deadlock.    
    /// </summary>
    public sealed class MultiThreadedSingleton
    {
    private static volatile MultiThreadedSingleton _instance;
    private static object syncRoot=new object(); private int _counter; public static MultiThreadedSingleton Instance
    {
    get
    {
    if (_instance==null)
    {
    lock (syncRoot)
    {
    if (_instance==null)
    {
    _instance=new MultiThreadedSingleton();
    _instance._counter=100;
    }
    }
    } _instance._counter++;
    return _instance;
    }
    } public int Counter
    {
    get
    {
    return _counter;
    }
    }
    } /// <summary>
    /// Class1 ??????
    /// </summary>
    class Client
    {
    /// <summary>
    /// Client
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    //
    // SingleThreadedSingleton:
    //
    // SingleThreadedSingleton stSingleton1=SingleThreadedSingleton.Instance;
    // Console.WriteLine("counter: {0}",stSingleton1.Counter.ToString());
    //
    // SingleThreadedSingleton stSingleton2=SingleThreadedSingleton.Instance;
    // Console.WriteLine("counter: {0}",stSingleton2.Counter.ToString()); //
    // MultiThreadedSingleton: you must test in multithreaded environment.
    // 
    MultiThreadedSingleton mtSingleton1=MultiThreadedSingleton.Instance;
    Console.WriteLine("counter: {0}",mtSingleton1.Counter.ToString()); MultiThreadedSingleton mtSingleton2=MultiThreadedSingleton.Instance;
    Console.WriteLine("counter: {0}",mtSingleton2.Counter.ToString());
    }
    }}
      

  4.   

    // Singleton// Intent: "Ensure a class only has one instance, and provide a global
    // point of access to it". // For further information, read "Design Patterns", p127, Gamma et al.,
    // Addison-Wesley, ISBN:0-201-63361-2/* Notes:
     * If it makes sense to have only a single instance of a class (a so-called
     * singleton), then it makes sense to enforce this (to elimintate potential 
     * errors, etc). 
     * 
     * A class based on the singleton design pattern protects its constructor, 
     * so that only the class itself (e.g. in a static method) may instantiate itself. 
     * It exposes an Instance method which allows client code to retrieve the 
     * current instance, and if it does not exist to instantiate it.  
     */
     
    namespace Singleton_DesignPattern
    {
        using System; class Singleton 
    {
    private static Singleton _instance;

    public static Singleton Instance()
    {
    if (_instance == null)
    _instance = new Singleton();
    return _instance;
    }
    protected Singleton(){} // Just to prove only a single instance exists
    private int x = 0;
    public void SetX(int newVal) {x = newVal;}
    public int GetX(){return x;}
    }    /// <summary>
        ///    Summary description for Client.
        /// </summary>
        public class Client
        {
            public static int Main(string[] args)
            {
                int val;
    // can't call new, because constructor is protected
    Singleton FirstSingleton = Singleton.Instance(); 
    Singleton SecondSingleton = Singleton.Instance(); // Now we have two variables, but both should refer to the same object
    // Let's prove this, by setting a value using one variable, and 
    // (hopefully!) retrieving the same value using the second variable
    FirstSingleton.SetX(4);
    Console.WriteLine("Using first variable for singleton, set x to 4"); val = SecondSingleton.GetX();
    Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);
                return 0;
            }
        }
    }
    或者看这个:
    http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=14719