Beispiel #1
0
        private void RunSingletonExample()
        {
            ChocolateBoiler boiler = ChocolateBoiler.GetChocolateBoiler();

            boiler.SomethingMethod();
            boiler.SomethingAnotherMethod();
        }
Beispiel #2
0
        private static void TestSingletonPattern()
        {
            ChocolateBoiler chocolateBoiler = ChocolateBoiler.Instance;

            Console.WriteLine(chocolateBoiler.Fill());
            Console.WriteLine(chocolateBoiler.Boil());
            Console.WriteLine(chocolateBoiler.Drain());
        }
        public static void Start()
        {
            ChocolateBoiler boiler = ChocolateBoiler.GetInstance();

            boiler.Fill();
            boiler.Boil();
            boiler.Drain();
        }
Beispiel #4
0
 public static ChocolateBoiler GetInstance()
 {
     if (Instance == null)
     {
         Instance = new ChocolateBoiler();
     }
     return(Instance);
 }
        public void TestInstanceCount()
        {
            ChocolateBoiler.GetInstance();
            ChocolateBoiler.GetInstance();
            var instance = ChocolateBoiler.GetInstance();

            Assert.AreEqual(1, ChocolateBoiler.count);
            instance.Fill();
        }
 public static ChocolateBoiler GetInstance()
 {
     if (uniqueInstance == null)
     {
         // lock uniqueInstance for multithreading
         lock (locker)
             uniqueInstance = new ChocolateBoiler();
     }
     return(uniqueInstance);
 }
Beispiel #7
0
        static void Main(string[] args)
        {
            ChocolateBoiler boiler = ChocolateBoiler.Instance;

            boiler.Fill();
            boiler.Boil();
            boiler.Drain();

            // will return the existing instance
            ChocolateBoiler boiler2 = ChocolateBoiler.Instance;
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            ChocolateBoiler chocolateBoiler = new ChocolateBoiler();

            chocolateBoiler.GetInstance();
            chocolateBoiler.Fill();
            chocolateBoiler.Boil();
            chocolateBoiler.Drain();

            Console.ReadLine();
        }
        public static IDesignPattern GetInstance(String patternName)
        {
            IDesignPattern designPattern;

            switch (patternName)
            {
            case DesignPatternConstants.SINGLETON:
                designPattern = ChocolateBoiler.GetSynchronizedInstance();
                break;

            case DesignPatternConstants.STRATEGY:
                designPattern = new MiniDuckSimulator();
                break;

            case DesignPatternConstants.OBSERVER:
                designPattern = new WeatherStation();
                break;

            case DesignPatternConstants.DECORATOR:
                designPattern = new StarbuzzCoffee();
                break;

            case DesignPatternConstants.SIMPLE_FACTORY:
                SimplePizzaFactory factory = new SimplePizzaFactory();
                designPattern = new SimpleFactory.PizzaStore.PizzaStore(factory);
                break;

            case DesignPatternConstants.FACTORY_METHOD:
                designPattern = new PizzaStoreFranchisor();
                break;

            case DesignPatternConstants.ABSTRACT_FACTORY:
                designPattern = new PizzaTestDrive();
                break;

            case DesignPatternConstants.COMMAND:
                designPattern = new RemoteLoader();
                break;

            case DesignPatternConstants.ADAPTER:
                designPattern = new DuckTestDrive();
                break;

            case DesignPatternConstants.FACADE:
                designPattern = new HomeTheaterTestDrive();
                break;

            default:
                throw new NotImplementedException();
            }

            return(designPattern);
        }
Beispiel #10
0
        public static ChocolateBoiler getChocolateBoiler()
        {
            lock (locker)
            {
                if (_boiler == null)
                {
                    _boiler = new ChocolateBoiler();
                }
            }

            return(_boiler);
        }
Beispiel #11
0
        /// <summary>
        /// O padrão Singleton garante que uma classe tenha apenas uma instância e fornece um ponto global de acesso a ela.
        /// </summary>
        private static void TestSingleton()
        {
            ChocolateBoiler boilerReference1 = ChocolateBoiler.GetInstance();
            ChocolateBoiler boilerReference2 = ChocolateBoiler.GetInstance();

            boilerReference1.Fill();
            boilerReference2.Fill();

            boilerReference1.Drain();

            boilerReference2.Boil();

            boilerReference1.Drain();
        }
 public static ChocolateBoiler GetBoiler()
 {
     if (_boiler == null)
     {
         lock (_syncRoot)
         {
             if (_boiler == null)
             {
                 _boiler = new ChocolateBoiler();
             }
         }
     }
     return(_boiler);
 }
        public static ChocolateBoiler GetInstance()
        {
            if (instance == null)
            {
                // Note that we use the lock the first time only
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new ChocolateBoiler();
                    }
                }
            }

            return(instance);
        }
Beispiel #14
0
        static void Main()
        {
            CheckNull();

            ChocolateBoiler boiler = ChocolateBoiler.GetInstance();

            CheckNull();

            //should work correct
            boiler.Fill();
            boiler.Boil();
            boiler.Drain();

            Console.WriteLine("\n");

            //should work incorrect
            boiler.Boil();
            boiler.Drain();
            boiler.Fill();
            boiler.Fill();
            boiler.Drain();

            Console.ReadKey();
        }
Beispiel #15
0
 public static ChocolateBoiler GetInstance()
 {
     return(_uniqueInstance ?? (_uniqueInstance = new ChocolateBoiler()));
 }