求问模式达人,静态类可否代替单件?

哈哈哈~看代码:)想必单件模式大家都非常熟悉:)
namespace Singleton1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
for (int i = 0; i < 10; i++)
            {
                Factory.RecordCount();
            }

            Factory.Display();

            global::System.Console.Read();
        }
    }

    
static class Factory
    {
        
private static object objLock = typeof(Factory);
        
private static long longCount = 0;

        
public static void RecordCount()
        {
            
lock (objLock)
            {
                longCount
++;
            }
        }

        
public static void Display()
        {
            
lock (objLock)
            {
                global::System.Console.WriteLine(longCount);
            }
        }
    }
}

posted on 2006-07-02 18:33  萝卜青菜  阅读(425)  评论(2编辑  收藏  举报

导航