Esempio n. 1
0
        static void Main(string[] args)
        {
            SingletonClass singleton = SingletonClass.Instance;

            singleton.Jest  = true;
            singleton.Nazwa = "Kłamca";

            Console.WriteLine(" Ksiązka pod tytułem  : " + singleton.Nazwa + "\n Czy najduje się w bazie : " + singleton.Jest + "\n");

            SingletonClass singleton2 = SingletonClass.Instance;

            singleton2.Jest = false;

            Console.WriteLine(" Ksiązka pod tytułem  : " + singleton2.Nazwa + "\n Czy najduje się w bazie : " + singleton2.Jest);

            Console.ReadKey();
        }
Esempio n. 2
0
 /// <summary>
 /// 3、定义公有方法来提供该类的唯一全局访问点
 /// </summary>
 /// <returns></returns>
 public static SingletonClass GetInstance()
 {
     // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
     // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
     // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
     // 双重锁定只需要一句判断就可以了
     if (unniqueInstance == null)
     {
         lock (locker)
         {
             // 如果类的实例不存在则创建,否则直接返回
             if (unniqueInstance == null)
             {
                 unniqueInstance = new SingletonClass();
             }
         }
     }
     return(unniqueInstance);
 }
Esempio n. 3
0
        /// <summary>
        /// The main Entry point of the project
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("Singleton pattern Demo");
            Console.WriteLine(SingletonClass.myInt);
            Console.WriteLine("Trying to create instance s1.");
            SingletonClass s1 = SingletonClass.Instance;

            Console.WriteLine("Trying to create instance s1");
            SingletonClass s2 = SingletonClass.Instance;


            if (s1 == s2)
            {
                Console.WriteLine("only one instance exists.");
            }
            else
            {
                Console.WriteLine("Different instance exists");
            }

            Console.Read();
        }
        public static SingletonClass GetInstance()
        {
            ///方法1: 多线程下不可取
            //if (_singleton == null)
            //{
            //    _singleton = new SingletonClass();
            //}
            ///方法2: 双重if 加锁的方式
            if (_singleton == null)
            {
                lock (lockObj)
                {
                    if (_singleton == null)
                    {
                        _singleton = new SingletonClass();
                    }
                }
            }



            return(_singleton);
        }
 private static void GetSingleton()
 {
     _instance = new SingletonClass();
 }
Esempio n. 6
0
 static void Main(string[] args)
 {
     SingletonClass sc = SingletonClass.GetInstance();
     //SingletonClass sc1 =new SingletonClass();
 }
Esempio n. 7
0
 public static void TestStaticAndSingleton(SingletonClass singleton)
 {
 }
Esempio n. 8
0
        private void Button_ShowClick(object sender, RoutedEventArgs e)
        {
            SingletonClass show = SingletonClass.Instance;

            MessageBox.Show(show.Ukaz());
        }
Esempio n. 9
0
        private void Button_SaveClick(object sender, RoutedEventArgs e)
        {
            SingletonClass save = SingletonClass.Instance;

            save.Pridat(name.Text, surename.Text, number.Text, date.Text);
        }