4、单例模式
namespace SJ.DesignMode { /// <summary> /// 设计模式 /// </summary> class Program { static void Main(string[] args) { SingleDistance s = SingleDistance.Create(); s.GetNum(); SingleDistance.num++; SingleDistance s2 = SingleDistance.Create(); s2.GetNum(); } } public class SingleDistance { private static SingleDistance _singleDistance = null; public static int num = 0; private SingleDistance() { } public static SingleDistance Create() { if (_singleDistance == null) { _singleDistance = new SingleDistance(); } return _singleDistance; } public void GetNum() { Console.WriteLine(num); } } }