Beispiel #1
0
        /// <summary>Zad A</summary>
        public void IsSameInstance()
        {
            SingletonLazy instance1 = SingletonLazy.Instance;
            SingletonLazy instance2 = SingletonLazy.Instance;

            Assert.AreSame(instance1, instance2);
        }
        public static void Run()
        {
            char key;

            while (true)
            {
                printMenu();

                key = Console.ReadKey().KeyChar;

                Console.WriteLine();
                switch (key)
                {
                case 'f':
                    FactoryMethodPatternExample.Display();
                    break;

                case 'a':
                    AbstractFactoryPatternExample.Display();
                    break;

                case 's':
                    SingletonPatternExample.Display();
                    SingletonLock.Display();
                    SingletonThreads.Display();
                    break;

                case 'l':
                    SingletonStaticFields.Display();
                    SingletonNestedClass.Display();
                    SingletonNestedLazy.Display();
                    break;

                case 'z':
                    SingletonLazy.Display();
                    break;

                case 'p':
                    PrototypePatternExample.Display();
                    PrototypePatternExampleComplex.Display();
                    break;

                case 'b':
                    BuilderPatternExample.Display();
                    break;

                case 'x': return;
                }
                Console.ReadKey();
            }
        }
Beispiel #3
0
 public static SingletonLazy GetInstance()
 {
     if (instance == null)
     {
         lock (synRoot)
         {
             if (instance == null)
             {
                 instance = new SingletonLazy();
             }
         }
     }
     return(instance);
 }
Beispiel #4
0
        public void GetInstance_MultipleCalls_InstanceCountMustBeOne(int threadCount)
        {
            Thread[] getInstanceThreads = new Thread[threadCount];

            for (int i = 0; i < threadCount; i++)
            {
                getInstanceThreads[i] = new Thread(() => { SingletonLazy.GetInstance(); });
            }

            Parallel.ForEach(getInstanceThreads, currentThread => currentThread.Start());
            SingletonLazy singletonInstance = SingletonLazy.GetInstance();

            Assert.AreEqual(1, singletonInstance.InstanceCount);
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            SingletonLazy instOne = SingletonLazy.GetLazyValue();

            SingletonLazy instTwo = SingletonLazy.GetLazyValue();
        }