void Awake()
 {
     // When this object wakes up, it sets the instance variable to
     // itself; because the instance variable is public and static, any
     // class from any location can access it and call its methods.
     instance = this;
 }
Exemple #2
0
        public async Task <string> GetSimpleSingleton()
        {
            var obj      = SimpleSingleton.GetInstance();
            var response = await obj.GetExample(_applicationEnvironment.ApplicationBasePath);

            return(response.Data);
        }
Exemple #3
0
            public void TestInstanceOfSimpleSingleton()
            {
                SimpleSingleton source = SimpleSingleton.Instance;

                SimpleSingleton target = SimpleSingleton.Instance;

                Assert.AreSame(source, target);
            }
Exemple #4
0
        private static void Main(string[] args)
        {
            Console.WriteLine("Singleton Mode Demo");

            SimpleSingleton.Instance();

            Singleton.Instance();

            ThreadSafeSingleton.Instance();
        }
Exemple #5
0
        public void SimpleSingletonTest()
        {
            //arrange
            var properResult = "It's me, singleton!";

            //act
            string result = SimpleSingleton.Execute();

            //assert
            Assert.AreEqual(properResult, result);
        }
 void Awake()
 {
     if (instance != null && instance != this)
     {
         Destroy(this.gameObject);
     }
     else
     {
         instance = this;
     }
 }
        public void TestSimpleSingleton()
        {
            SimpleSingleton anyClass = SimpleSingleton.GetInstance;

            anyClass.AnyMethod("My anyClass call instance of singleton");

            SimpleSingleton anyClass2 = SimpleSingleton.GetInstance;

            anyClass2.AnyMethod("My anyClass2 use the same instance of singleton");

            Assert.AreSame(anyClass, anyClass2);
        }
Exemple #8
0
        public void SingletonTest()
        {
            SimpleSingleton s1 = SimpleSingleton.CreateInstance();
            SimpleSingleton s2 = SimpleSingleton.CreateInstance();

            Singleton s3 = Singleton.Instance;

            int r1 = s1.getRandomNumber();
            int r2 = s2.getRandomNumber();

            Assert.AreSame(s1, s2);
        }
Exemple #9
0
        static void Main(string[] args)
        {
            SimpleSingleton singleton = new SimpleSingleton();

            string message = $"**** First instance is [{singleton.GetHashCode()}] ****";

            System.Diagnostics.Debug.WriteLine(message);

            singleton = new SimpleSingleton();

            message = $"**** Last instance is [{singleton.GetHashCode()}] ****";
            System.Diagnostics.Debug.WriteLine(message);
        }
Exemple #10
0
 private void Awake()
 {
     if (instance != null)
     {
         Debug.Log("Destroy " + Tag + " " + gameObject.name);
         Destroy(gameObject);
     }
     else
     {
         Debug.Log("Assign this " + Tag + " instance  with name " + gameObject.name);
         instance = this;
     }
 }
    void Awake()
    {
        if (instance != null && instance != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            instance = this;
        }

        DontDestroyOnLoad(this.gameObject);
    }
Exemple #12
0
 public static SimpleSingleton Instance()
 {
     if (_instance == null)
     {
         lock (_lock)
         {
             if (_instance == null)
             {
                 _instance = new SimpleSingleton();
             }
         }
     }
     return(_instance);
 }
Exemple #13
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Creational Patterns deals with object creation!");
            Console.WriteLine("<---------------------------- Singleton ---------------------------->");
            Parallel.Invoke(() => { SimpleSingleton simpleSingleton = SimpleSingleton.Instance; },
                            () => { SimpleSingleton simpleSingleton = SimpleSingleton.Instance; });
            Parallel.Invoke(() => { ThreadSafeSingleton threadSafeSingleton = ThreadSafeSingleton.Instance; },
                            () => { ThreadSafeSingleton threadSafeSingleton = ThreadSafeSingleton.Instance; });
            Parallel.Invoke(() => { LazySingleton lazySingleton = LazySingleton.Instance; },
                            () => { LazySingleton lazySingleton = LazySingleton.Instance; });

            Console.WriteLine("<---------------------------- Simple Factory ---------------------------->");
            SimpleFactory.ICreditCard card = SimpleFactory.CreditCardFactory.GetCreditCard(SimpleFactory.CreditCardType.Platinum);
            Console.WriteLine($"Credit card Type: {card.GetCardType()}");
            Console.WriteLine($"Credit card Limit: {card.GetLimit()}");
            Console.WriteLine($"Credit card AnnualCharges: {card.GetAnnualCharges()}");

            card = SimpleFactory.CreditCardFactory.GetCreditCard(SimpleFactory.CreditCardType.Titanium);
            Console.WriteLine($"Credit card Type: {card.GetCardType()}");
            Console.WriteLine($"Credit card Limit: {card.GetLimit()}");
            Console.WriteLine($"Credit card AnnualCharges: {card.GetAnnualCharges()}");

            Console.WriteLine("<---------------------------- Factory Method ---------------------------->");
            FactoryMethod.CreditCardFactory creditCardFactory = new PlatinumFactory();
            FactoryMethod.ICreditCard       creditCard        = creditCardFactory.GetCreditCard();
            Console.WriteLine($"Credit card Type: {creditCard.GetCardType()}");
            Console.WriteLine($"Credit card Limit: {creditCard.GetLimit()}");
            Console.WriteLine($"Credit card AnnualCharges: {creditCard.GetAnnualCharges()}");

            creditCardFactory = new TitaniumFactory();
            creditCard        = creditCardFactory.GetCreditCard();
            Console.WriteLine($"Credit card Type: {creditCard.GetCardType()}");
            Console.WriteLine($"Credit card Limit: {creditCard.GetLimit()}");
            Console.WriteLine($"Credit card AnnualCharges: {creditCard.GetAnnualCharges()}");

            Console.WriteLine("<---------------------------- Abstract Factory ---------------------------->");
            ComputerFactory computerFactory = new ComputerFactory(new ExpensiveComputer());

            computerFactory.Assemble();
            computerFactory = new ComputerFactory(new CheapComputer());
            computerFactory.Assemble();

            Console.WriteLine("<---------------------------- Builder ---------------------------->");
            Builder.IComputer computer = new Builder.LaptopBuilder();
            computer.AddCPU("Intel I3");
            computer.AddGPU("AMD Rideon");
            computer.AddRAM("8 GB");
            computer.AddHDD("SATA 500 GB");
            computer.EanbleTouchscreen(true);
            computer.DisplayDetails();

            computer = new Builder.DesktopBuilder();
            computer.AddCPU("Intel I5");
            computer.AddGPU("NVidea");
            computer.AddRAM("16 GB");
            computer.AddHDD("SATA 1 TB");
            computer.AddMonitor("Full HD");
            computer.DisplayDetails();

            Console.WriteLine("<---------------------------- Fluent Builder ---------------------------->");
            FluentBuilder.IComputer computer1 = new FluentBuilder.LaptopBuilder();
            computer1.AddCPU("Intel I3")
            .AddGPU("AMD Rideon")
            .AddRAM("8 GB")
            .AddHDD("SATA 500 GB")
            .EanbleTouchscreen(true)
            .DisplayDetails();

            computer1 = new FluentBuilder.DesktopBuilder();
            computer1.AddCPU("Intel I5")
            .AddGPU("NVidea")
            .AddRAM("16 GB")
            .AddHDD("SATA 1 TB")
            .AddMonitor("Full HD")
            .DisplayDetails();

            Console.ReadLine();
        }