コード例 #1
0
        static void Main(string[] args)
        {
            ISandwich sandwich1 = SandwichMaker
                                  .CreateSandwich(SandwichType.Bauru, true, true)
                                  .BuildSandwich()
                                  .GetSandwhich();

            sandwich1.Display();

            ISandwich sandwich2 = SandwichMaker
                                  .CreateSandwich(SandwichType.Chacarero, false, true)
                                  .BuildSandwich()
                                  .GetSandwhich();

            sandwich2.Display();

            ISandwich sandwich3 = SandwichMaker
                                  .CreateSandwich(SandwichType.Jambon)
                                  .BuildSandwich()
                                  .GetSandwhich();

            sandwich3.Display();

            Console.ReadLine();
        }
コード例 #2
0
        public static void Builder()
        {
            var builder = new SandwichMaker(new ClubSandwichSandwichBuilder());

            builder.BuildSandwich();
            var sandwich = builder.GetSandwich();
        }
コード例 #3
0
        public SandwichMaker SandwichMaker(string sandwichName)
        {
            SandwichBuilder sandwichBuilder = new SandwichFactory().CreateInstance(sandwichName);

            SandwichMaker sandwichMaker = new SandwichMaker(sandwichBuilder);

            return(sandwichMaker);
        }
コード例 #4
0
        static void Main(string[] args)
        {
            Console.WriteLine("Programs that have too many parameters are often a good candidate for the builder pattern!");

            var myBloatedParameterSandwich = new WithoutBuilderPattern.Sandwich(BreadType.Wheat, true, CheeseType.American, MeatType.Chicken, false, true, new List <string>()
            {
                "carrots", "peppers"
            });

            myBloatedParameterSandwich.Display();

            //Again with this approach the responsibility is on the client code to get it right.  It must know which parameters/properties it must set and have exposed to it.
            //the types/enums inner workings of the Sandwich class.
            var mySlightyBetterCtorLessParametersGuessingSandwich = new WithoutCtorButWithPublicParameters.Sandwich();

            mySlightyBetterCtorLessParametersGuessingSandwich.BreadType  = BreadType.Wheat;
            mySlightyBetterCtorLessParametersGuessingSandwich.CheeseType = CheeseType.American;

            //The other boolean parameters  such as:

            /*
             * isToasted, hasMustard, hasMayo => will all have default values of false.
             *
             * The importance of the client setting them or not is unknown.
             */

            //Also if the client forgets to set just one of the many parameters/properties on the complex class then bugs can occur.
            //For instance if we were to comment out the below lines then a runtime exception of null will occur due to the vegetables not being set to an empty list.
            //mySlightyBetterCtorLessParametersGuessingSandwich.Vegetables = new List<string>() {"carrots", "peppers"};
            mySlightyBetterCtorLessParametersGuessingSandwich.Vegetables = new List <string>()
            {
                "carrots", "peppers"
            };

            mySlightyBetterCtorLessParametersGuessingSandwich.Display();

            //Now to use the Builder Pattern.  note: How the client concerns of methods above have been extracted away.
            //These builder classes could have in themselves, setting or content services injected into them to allow them to build more line of business objects
            //By coding in such a way as to use the builder pattern, accidentally forgetting to set the vegetables, breadType, cheeseType etc is not possible.
            //This is due to the builders logic which is encapsulated.
            var turkeyCheesSwizzlerSandwichMaker = new SandwichMaker(new TurkeyCheesSwizzlerSandwichBuilder());

            turkeyCheesSwizzlerSandwichMaker.BuildSandwich();
            var turkeyCheesSwizzlerSandwich = turkeyCheesSwizzlerSandwichMaker.GetSandwhich();

            turkeyCheesSwizzlerSandwich.Display();

            //Now another variety of sandwich.

            var clubSandwichMaker = new SandwichMaker(new ClubSandwichBuilder());

            clubSandwichMaker.BuildSandwich();
            var clubSandwich = clubSandwichMaker.GetSandwhich();

            clubSandwich.Display();

            Console.ReadLine();
        }
コード例 #5
0
        public void MySandwichBuilder()
        {
            var sandwichMaker = new SandwichMaker(new MySandwichBuilder());

            sandwichMaker.Build();
            var sandwich = sandwichMaker.Sandwich();

            Assert.DoesNotThrow(() => sandwich.Display());
        }
コード例 #6
0
        public void Make_Turkey_Sandwich()
        {
            var builder = new TurkeySandwichBuilder();
            var maker   = new SandwichMaker(builder);

            maker.Build();
            var sandwich = maker.GetSandwich();

            Assert.Equal("Toasted: True | Mustard: False | Mayo: True | Meat: Turkey | Cheese: Cheddar", sandwich.ToString());
        }
コード例 #7
0
        public void Make_Ham_Sandwich()
        {
            var builder = new HamSandwichBuilder();
            var maker   = new SandwichMaker(builder);

            maker.Build();
            var sandwich = maker.GetSandwich();

            Assert.Equal("Toasted: True | Mustard: True | Mayo: False | Meat: Ham | Cheese: Swiss", sandwich.ToString());
        }
コード例 #8
0
        public void Test_UseBuilderPatternExample()
        {
            SandwichBuilder builder = new MyFavoriteSandwichBuilder();
            SandwichMaker   maker   = new SandwichMaker(builder);

            maker.BuildSandwich();
            var sandwich = maker.GetSandwich();

            Assert.IsNotNull(sandwich);
            Assert.IsNotEmpty(sandwich.Vegatables); // MyFavoriteSandwichBuilder has two vegetables added
            Assert.AreEqual(2, sandwich.Vegatables.Count);
        }
コード例 #9
0
        static void Main(string[] args)
        {
            Facade appFacade = new Facade();


            Console.WriteLine("What kind of sandwich you want?");
            string sandwichName = Console.ReadLine();

            SandwichMaker Sandwich = appFacade.SandwichMaker(sandwichName);

            Sandwich.BuildSandwich();
            Sandwich.GetSandwhich().Display();


            Console.ReadKey();
        }
コード例 #10
0
        private void BuilderReal()
        {
            var sandwitchMaker = new SandwichMaker(new StandartSandwichBuilder());

            sandwitchMaker.BuildSandwich();
            var standartSandwich = sandwitchMaker.GetSandwich();

            standartSandwich.Display();
            Console.ReadLine();

            sandwitchMaker = new SandwichMaker(new ClubSandwichBuilder());
            sandwitchMaker.BuildSandwich();
            var clubSandwich = sandwitchMaker.GetSandwich();

            clubSandwich.Display();
            Console.ReadLine();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: agarwal-peeush/Learning
        private static void DemoBuilderPattern()
        {
            //Problem 1: Complex Sandwich ctor and it can grow long and long
            //new Sandwich_Original(BreadType.Wheat, false, CheeseType.American, MeatType.Turkey, false, false, new List<string> { "Tomato" }).Display();

            //    Solution to Problem 1: Convert ctor variables to Properties to reduce parameters in Sandwich ctor.
            //Problem 2: Now, after converting to properties, we have to remember each property to set
            //Problem 3: We have lost the Order at which Properties needs to be set.
            //var sandwich = new Sandwich()
            //{
            //    BreadType = BreadType.Wheat,
            //    IsToasted = false,
            //    CheeseType = CheeseType.American,
            //    MeatType = MeatType.Turkey,
            //    HasMayo = false,
            //    HasMustard = false,
            //    Vegetables = new List<string> { "Tomato" },
            //};
            //sandwich.Display();

            //    Solution to Problem 2 and 3: Create SandwichBuilder which creates an object of Sandwich and method to return created object.
            //Problem 4: If we introduce new Builder, it doesn't know steps to build a sandwich.
            //var builder = new MySandwichBuilder();
            //builder.CreateSandwich();
            //var sandwich = builder.GetSandwich();
            //sandwich.Display();

            //      Solution to Problem 4: Create SandwichMaker which handles the process/steps of building a Sandwich. It uses the builder passed to build a Sandwich
            SandwichMaker maker = new SandwichMaker(new MySandwichBuilder());

            maker.BuildSandwich();
            Sandwich sandwich = maker.GetSandwich();

            sandwich.Display();


            maker = new SandwichMaker(new ClubSandwichBuilder());
            maker.BuildSandwich();
            sandwich = maker.GetSandwich();
            sandwich.Display();
        }
コード例 #12
0
        public ActionResult Get()
        {
            try
            {
                List <ApiResult> results = new List <ApiResult>();
                var sandwichMaker        = new SandwichMaker(new ClassicSandwich());
                sandwichMaker.BuildSandwich();
                results.Add(_mapper.Map <ApiResult>(sandwichMaker.GetSandwhich()));

                var sandwichMaker2 = new SandwichMaker(new ClubSandwich());
                sandwichMaker2.BuildSandwich();
                results.Add(_mapper.Map <ApiResult>(sandwichMaker2.GetSandwhich()));

                return(Ok(results));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.ToString());
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }