求问模式达人,静态类可否代替单件?
哈哈哈~看代码:)想必单件模式大家都非常熟悉:)
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);
}
}
}
}
{
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);
}
}
}
}