Exemplo n.º 1
0
        static void Main(string[] args)
        {
            //  DESIGN PATTERNS
            // =====================
            //
            //  Factory Method (or just Factory)
            // have a factory class create the derived class for you, think of what a factory does, it creates objects for you.

            var dogFactory = new DogFactory();

            var dog = dogFactory.Create(DogType.Poodle);

            Console.WriteLine(dog.Bark());
            Console.WriteLine();

            // Singleton
            // Purpose: have a single instance of an objct in memory, great if you want a single source of truth, and or you ONLY want
            // one instance of an object into memory. now a days, this is a popular one to have around.

            var appOptions = AppOptions.Instance;

            appOptions.ApplicationOwner = "James";
            appOptions.InCloud          = false;

            var appOptionsTwo = AppOptions.Instance;

            Console.WriteLine(appOptionsTwo.ApplicationOwner);
            Console.WriteLine();

            // Fluent Builder
            // Purpose: Another common Dotnet Creation pattern, for building out your object

            var carBuilder = new CarBuilder();

            var car = carBuilder.Add4WheelDrive()
                      .AddConvertable()
                      .AddWarrinty(5)
                      .AddOwner("Jimmy")
                      .Build();

            Console.WriteLine(car.Owner);
            Console.WriteLine(car.WarrintyInYears);

            Console.ReadLine();
        }
Exemplo n.º 2
0
        private void simpleFactoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdateOutput("A factory is called and will return an instance following an interface definition, no abstract class used.");

            DogFactory dogFactory = new DogFactory();
            IDog       dog;

            dog = dogFactory.GetDog(Dogs.Collie);
            UpdateOutput(dog.Bark());

            dog = dogFactory.GetDog(Dogs.Poodle);
            UpdateOutput(dog.Bark());

            dog = dogFactory.GetDog(Dogs.Wolfhound);
            UpdateOutput(dog.Bark());

            UpdateOutput("");
        }