コード例 #1
0
        static void Main(string[] args)
        {
            Beverage beverage1 = new Espresso();

            Console.WriteLine(beverage1);

            Beverage beverage1Small = new Espresso();

            beverage1Small.SetSize(CupSize.Small);
            Console.WriteLine(beverage1Small);

            Beverage beverage2 = new DarkRoast();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2);

            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine(beverage3);

            Beverage beverage3Big = new HouseBlend(CupSize.Big);

            beverage3Big = new Soy(beverage3Big);
            beverage3Big = new Mocha(beverage3Big);
            beverage3Big = new Whip(beverage3Big);
            Console.WriteLine(beverage3Big);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: KileyDowling/Sandbox
        static void Main(string[] args)
        {
            Beverage beverage = new Expresso();

            Console.WriteLine("Beverage #1: " + beverage.getDescription() + " $" + beverage.cost());


            Beverage beverage2 = new DarkRoast();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);

            Console.WriteLine("\nBeverage #2: " + beverage2.getDescription() + " $" + beverage2.cost());


            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);

            Console.WriteLine("\nBeverage #3: " + beverage3.getDescription() + " $" + beverage3.cost());


            //keep window open
            Console.ReadLine();
        }
コード例 #3
0
        public void CostShouldReturnEigthyNineCents()
        {
            var expectedValue = 0.89M;
            var houseBlend    = new HouseBlend();

            houseBlend.Cost().Should().Be(expectedValue);
        }
コード例 #4
0
        /// <summary>
        /// # Motivation
        /// I can now attach responsibilities to an object dynamically to add behaviour at runtime.
        ///
        /// # Structure
        /// Beverage
        ///  --> base class so all objects are of same type
        /// CondimentDecorator : Bevarage
        ///  --> interchangeable with Beverage
        ///
        /// # Sample Problem
        /// A coffee shop wants to make an ordering system for all their drinks. Ofcourse there are a lot of combination with condiments/roasts.
        /// We could create a base class and let all possible combinations inherit from the superclass and override their behaviour, but that would quickly become a mess.
        ///
        /// # Solution
        /// By creating a Decorator class which inherits from the baseclass (to effectively swap between types), I can now dynamically create composite objects at runtime.
        /// To add a drink to the system:
        ///     1. inherit from the base class, set instance fields in constructor
        ///     2. override the 'Cost()' method to return desired cost
        /// To add a condiment to the system:
        ///     1. inherit from 'CondimentDecorator' (indirectly from base class as well
        ///     2. add instance field from type Beverage
        ///     3. initialize in constructor
        ///     4. override GetDescription() and Cost() accordingly
        /// To create a composite object at runtime:
        ///     1. Create a new instance of a drink (Beverage)
        ///     2. To add a condiment, assign the instance to a new instance of the condiment with itself as param
        ///         A. Beverage darkRoast = new DarkRoast();
        ///         B. darkRoast = new Mocha(darkRoast);
        /// </summary>
        public void Test()
        {
            Beverage beverage = new Espresso();

            Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}");

            Beverage beverage1 = new DarkRoast();

            beverage1 = new Mocha(beverage1);
            Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}");

            Beverage beverage2 = new DarkRoast();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine($"{beverage2.GetDescription()} ${beverage2.Cost()}");

            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine($"{beverage3.GetDescription()} ${beverage3.Cost()}");
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Beverage beverage = new Expresso();

            Console.WriteLine(beverage.GetDescription() + " R$" + beverage.Cost());
            Console.WriteLine();

            Beverage beverage2 = new DarkRoast();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2.GetDescription() + " R$" + beverage2.Cost());
            Console.WriteLine();

            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine(beverage3.GetDescription() + " R$" + beverage3.Cost());
            Console.WriteLine();

            Console.ReadKey();
        }
コード例 #6
0
        public void DecoratorTest()
        {
            DIServiceLocator.Current.RegisterInstance(typeof(ITextProvider));
            Support.Configure();

            Beverage beverage = new Espresso();

            beverage = new Mocha(beverage);
            Assert.AreEqual(beverage.Description, "Espresso, Mocha");
            Assert.AreEqual(beverage.Cost(), 2.19);

            Beverage beverage2 = new DarkRoast();

            beverage2 = beverage2.AddComponent <Mocha>();
            beverage2 = beverage2.AddComponent <Mocha>();
            beverage2 = beverage2.AddComponent <Whip>();
            Assert.AreEqual(beverage2.Description, "Dark Roast, Mocha, Mocha, Whip");
            Assert.AreEqual(beverage2.Cost(), 1.49);

            Beverage beverage3 = new HouseBlend();

            SupportExtensions.AddComponent <Soy>(ref beverage3);
            SupportExtensions.AddComponent <Mocha>(ref beverage3);
            SupportExtensions.AddComponent <Whip>(ref beverage3);
            Assert.AreEqual(beverage3.Description, "House Blend Coffee, Soy, Mocha, Whip");
            Assert.AreEqual(beverage3.Cost(), 1.34);
        }
コード例 #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Strategy Pattern");
            var modelDuck = new ModelDuck();

            modelDuck.PerformFly();
            modelDuck.PerformQuack();
            modelDuck.SetFlyBehavior(new ModelFly());
            modelDuck.PerformFly();

            Console.WriteLine("Observer Patterns!");
            var weatherData = new WeatherData();
            var observerone = new OberverElement(weatherData);

            observerone.Subscript();
            var observertwo = new OberverOtherElement(weatherData);

            observertwo.Subscript();
            weatherData.SetMeasurementsChanged((float)35.50, (float)20.00, (float)264.00);

            observerone.UnSubscript();
            weatherData.SetMeasurementsChanged((float)37.50, (float)30.00, (float)260.00);

            Console.WriteLine("Decorator Patterns");
            var beverage = new Espresso();

            Console.WriteLine(beverage.GetDescription());

            BeverageBase beverage2 = new HouseBlend();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2.GetDescription() + "$" + beverage2.Cost());
        }
コード例 #8
0
        public void GetDescriptionShouldReturnDescriptionWithTextSettedInConstructor()
        {
            var expectedValue = "House Blend Coffee";
            var houseBlend    = new HouseBlend();

            houseBlend.GetDescription().Should()
            .Be(expectedValue);
        }
コード例 #9
0
        public void HouseBlendWithSoyWithMochaWithWhip()
        {
            Beverage beverage = new HouseBlend();
            beverage = new Soy(beverage);
            beverage = new Mocha(beverage);
            beverage = new Whip(beverage);

            Assert.AreEqual(1.34, beverage.Cost(),DELTA);
        }
コード例 #10
0
        public void HouseBlendWithSoyWithMochaWithWhip()
        {
            HouseBlend beverage = new HouseBlend();
            beverage.HasSoy = true;
            beverage.HasMocha = true;
            beverage.HasWhip = true;

            Assert.AreEqual(1.34, beverage.Cost(), DELTA);
        }
        public void TestHouseBlend()
        {
            Beverage beverage = new HouseBlend();

            beverage = new Mocha(beverage);
            beverage = new SteamedMilk(beverage);
            Assert.AreEqual("House Blend Coffee, Mocha, Steamed Milk $1.09", beverage.GetDescription() +
                            " $" + beverage.Cost());
        }
コード例 #12
0
        public void HouseBlendWithSoyWithMochaWithWhip()
        {
            Beverage beverage = new HouseBlend();

            beverage = new Soy(beverage);
            beverage = new Mocha(beverage);
            beverage = new Whip(beverage);

            Assert.AreEqual(1.34, beverage.Cost(), DELTA);
        }
コード例 #13
0
        public void HouseBlendWithSoyWithMochaWithWhip()
        {
            HouseBlend beverage = new HouseBlend();

            beverage.HasSoy   = true;
            beverage.HasMocha = true;
            beverage.HasWhip  = true;

            Assert.AreEqual(1.34, beverage.Cost(), DELTA);
        }
コード例 #14
0
        public static void Run()
        {
            Beverage beverage = new Espresso();

            Console.WriteLine(beverage.description + " $" + beverage.cost());
            Console.WriteLine();

            Beverage beverage2 = new DarkRoast();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Mocha(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2.description + " $" + beverage2.cost());
            Console.WriteLine();

            Beverage beverage3 = new HouseBlend();

            beverage3 = new Soy(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine(beverage3.description + " $" + beverage3.cost());
            Console.WriteLine();

            Beverage beverage4 = new HouseBlend();

            beverage4.size = Beverage.Size.GRANDE;
            Console.WriteLine("Setting size to: {0}", beverage4.size);
            beverage4 = new Soy(beverage4);
            beverage4 = new Mocha(beverage4);
            beverage4 = new Whip(beverage4);
            Console.WriteLine(beverage4.description + " $" + beverage4.cost());
            Console.WriteLine();

            Beverage beverage5 = new HouseBlend();

            beverage5.size = Beverage.Size.TALL;
            Console.WriteLine("Setting size to: {0}", beverage5.size);
            beverage5 = new Soy(beverage5);
            beverage5 = new Mocha(beverage5);
            beverage5 = new Whip(beverage5);
            Console.WriteLine(beverage5.description + " $" + beverage5.cost());
            Console.WriteLine();

            Beverage beverage6 = new HouseBlend();

            beverage6.size = Beverage.Size.VENTI;
            Console.WriteLine("Setting size to: {0}", beverage6.size);
            beverage6 = new Soy(beverage6);
            beverage6 = new Mocha(beverage6);
            beverage6 = new Whip(beverage6);
            Console.WriteLine(beverage6.description + " $" + beverage6.cost());
            Console.WriteLine();

            Console.ReadKey();
        }
コード例 #15
0
        public void HouseBlendSoyMochaWhip()
        {
            IBeverage houseBlend = new HouseBlend(BeverageSize.Small);

            houseBlend = new Soy(houseBlend);
            houseBlend = new Whip(houseBlend);
            houseBlend = new Mocha(houseBlend);

            Debug.WriteLine(houseBlend.Description);
            Debug.WriteLine("Cost: " + houseBlend.Cost);
        }
コード例 #16
0
ファイル: UnitTest1.cs プロジェクト: kbussert/Visual_Studio
        public void HouseBlendPriceTest()
        {
            //Arrange
            Beverage bev = new HouseBlend();
            decimal  price;

            //Act
            price = bev.GetPrice();
            //Assert
            Assert.AreEqual(price, 0.89m);
        }
コード例 #17
0
        public void HouseBlend_WhenAddDecorators_ValueIsIncreased()
        {
            Beverage houseBlend = new HouseBlend();
            decimal  value      = houseBlend.Value();

            houseBlend = new Soy(houseBlend);
            houseBlend = new Mocha(houseBlend);

            Assert.NotEqual(value, houseBlend.Value());

            Assert.Equal(houseBlend.Description, houseBlend.GetDescription());
        }
コード例 #18
0
        public void CanGetDescriptionOfBeverage()
        {
            // Arrange

            var houseBlend = new HouseBlend();

            // Act
            var description = houseBlend.Description;

            // Assert
            Assert.That(description, Is.EqualTo("House Blend"));
        }
コード例 #19
0
        public void CondimentsDescriptionShouldBeApplied()
        {
            var expected = "This is a HouseBlend with Almond Milk";

            IBeverage houseBlend = new HouseBlend();

            houseBlend = new AlmondMilk(houseBlend);

            var result = houseBlend.GetDescription();

            result.Should().Be(expected);
        }
コード例 #20
0
        public void DoubleMochaSoyLatteWithWhip()
        {
            IBeverage houseBlend = new HouseBlend(BeverageSize.Medium);

            houseBlend = new Soy(houseBlend);
            houseBlend = new Whip(houseBlend);
            houseBlend = new Mocha(houseBlend);
            houseBlend = new Mocha(houseBlend);

            Debug.WriteLine(houseBlend.Description);
            Debug.WriteLine("Cost: " + houseBlend.Cost);
        }
コード例 #21
0
        public void CondimentsCostShouldBeApplied()
        {
            var expected = 3.2;

            IBeverage houseBlend = new HouseBlend();

            houseBlend = new AlmondMilk(houseBlend);

            var result = houseBlend.Cost();

            result.Should().Be(expected);
        }
コード例 #22
0
ファイル: Program.cs プロジェクト: Thimoderam/Design-Patterns
        private static void RunDecoratorPattern()
        {
            Beverage beverage = new Espresso();

            Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}");
            Beverage beverage1 = new HouseBlend();

            beverage1 = new Mocha(beverage1);
            beverage1 = new Mocha(beverage1);
            beverage1 = new Whip(beverage1);
            Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}");
        }
コード例 #23
0
        public void HouseBlend_ShouldBeExpressoAndCostEightyNineCents_WhenIsInstantiate()
        {
            //Arrange
            Beverage beverage = new HouseBlend();

            //Act
            var description = beverage.GetDescription();
            var cost        = beverage.Cost();

            //Assert
            description.Should().Be("House Blend Coffee");
            cost.Should().Be(.89);
        }
コード例 #24
0
ファイル: Program.cs プロジェクト: W3SS/dotnet-designpatterns
        public static void Decorator()
        {
            var espresso = new Espresso();

            Console.WriteLine($"{espresso.Description} ${espresso.Cost()}");

            Beverage houseblend = new HouseBlend();

            houseblend = new Mocha(houseblend);
            houseblend = new Mocha(houseblend);
            houseblend = new Soy(houseblend);
            Console.WriteLine($"{houseblend.Description} ${houseblend.Cost()}");
        }
コード例 #25
0
        public void StarBuzzCoffee()
        {
            Beverage espresso = new Espresso();

            Console.WriteLine(espresso.GetDescription() + " $" + espresso.Cost());

            Beverage houseBlend = new HouseBlend();

            houseBlend = new Soy(houseBlend);
            houseBlend = new Mocha(houseBlend);
            houseBlend = new Whip(houseBlend);
            Console.WriteLine(houseBlend.GetDescription() + " $" + houseBlend.Cost());
        }
コード例 #26
0
ファイル: UnitTest1.cs プロジェクト: kbussert/Visual_Studio
        public void HouseBlendWithMochaPriceTest()
        {
            //Arrange
            Beverage bev = new HouseBlend();

            bev = new Mocha(bev);
            decimal price;

            //Act
            price = bev.GetPrice();
            //Assert
            Assert.AreEqual(price, 1.09m);
        }
コード例 #27
0
ファイル: Program.cs プロジェクト: cunhaalves/design-patterns
        static void RunDecorator()
        {
            Beverage beverage = new Expresso();

            Console.WriteLine(beverage.GetDescription() + " $ " + beverage.Cost());

            Beverage beverage2 = new HouseBlend();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Soy(beverage2);
            beverage2 = new Whip(beverage2);

            Console.WriteLine(beverage2.GetDescription() + " $ " + beverage2.Cost());
        }
コード例 #28
0
        static void Main(string[] args)
        {
            var beverage = new DarkRoast();

            Console.WriteLine($"Description: {beverage.GetDescription()}; Cost: {beverage.Cost()}");

            var beverage2  = new HouseBlend();
            var decorator1 = new Mocha(beverage2);
            var decorator2 = new Milk(decorator1);

            Console.WriteLine($"Description: {decorator2.GetDescription()}; Cost: {decorator2.Cost()}");

            Console.ReadKey();
        }
コード例 #29
0
        static void Main(string[] args)
        {
            Beverage essp = new Espresso();

            Console.WriteLine(essp.GetDiscription() + "\t" + essp.cost().ToString());

            Beverage houseB = new HouseBlend();

            Console.WriteLine(houseB.GetDiscription() + "\t" + houseB.cost().ToString());

            essp = new Whip(essp);
            Console.WriteLine(essp.GetDiscription() + "\t" + essp.cost().ToString());

            essp = new SteamMilk(essp);
            Console.WriteLine(essp.GetDiscription() + "\t" + essp.cost().ToString());

            houseB = new Soy(houseB);
            Console.WriteLine(houseB.GetDiscription() + "\t" + houseB.cost().ToString());

            // Create book
            Book book = new Book("Worley", "Inside ASP.NET", 10);

            book.Display();
            // Create video
            Video video = new Video("Spielberg", "Jaws", 23, 92);

            video.Display();
            // Make video borrowable, then borrow and display

            Console.WriteLine("\nMaking video borrowable:");

            Borrowable borrowvideo = new Borrowable(video);

            borrowvideo.BorrowItem("Customer #1");
            borrowvideo.BorrowItem("Customer #2");
            borrowvideo.BorrowItem("Customer #3");
            borrowvideo.Display();

            borrowvideo.ReturnItem("Customer #1");
            borrowvideo.ReturnItem("Customer #2");
            borrowvideo.Display();

            // Wait for user
            Console.ReadKey();

            while (true)
            {
            }
        }
コード例 #30
0
        static void Main(string[] args)
        {
            Beverage beverage = new Espresso();

            Console.WriteLine($"{beverage.GetDescription()} ${beverage.Cost()}");

            Beverage beverage1 = new HouseBlend();

            beverage1 = new Soy(beverage1);
            beverage1 = new Mocha(beverage1);
            beverage1 = new Mocha(beverage1);
            Console.WriteLine($"{beverage1.GetDescription()} ${beverage1.Cost()}");

            Console.ReadLine();
        }
コード例 #31
0
ファイル: UnitTest1.cs プロジェクト: kbussert/Visual_Studio
        public void DoubleMochaSoyLatteWithWhipPriceTest()
        {
            //Arrange
            Beverage bev = new HouseBlend();

            bev = new Soy(bev);
            bev = new Mocha(bev);
            bev = new Mocha(bev);
            bev = new Whip(bev);
            decimal price;

            //Act
            price = bev.GetPrice();
            //Assert
            Assert.AreEqual(price, 1.54m);
        }
コード例 #32
0
ファイル: Program.cs プロジェクト: ClintSu/DesignPattern
        static void Main(string[] args)
        {
            Beverage beverage = new Espresso();

            Console.WriteLine(beverage.GetDescription() + " $" + beverage.Cost());

            Beverage beverage2 = new HouseBlend();

            beverage2 = new Mocha(beverage2);
            beverage2 = new Soy(beverage2);
            beverage2 = new Whip(beverage2);
            Console.WriteLine(beverage2.GetDescription() + " $" + beverage2.Cost());

            Beverage beverage3 = new DarkRoase();

            beverage3 = new Mocha(beverage3);
            beverage3 = new Mocha(beverage3);
            beverage3 = new Whip(beverage3);
            Console.WriteLine(beverage3.GetDescription() + " $" + beverage3.Cost());

            //Console.ReadLine();
            Console.WriteLine("---------------------\n");

            // 我买了个苹果手机
            Phone phone = new ApplePhone();
            // 现在想贴膜了
            Decorator applePhoneWithSticker = new Sticker(phone);

            // 扩展贴膜行为
            applePhoneWithSticker.Print();
            Console.WriteLine("----------------------\n");

            // 现在我想有挂件了
            Decorator applePhoneWithAccessories = new Accessories(phone);

            // 扩展手机挂件行为
            applePhoneWithAccessories.Print();
            Console.WriteLine("----------------------\n");

            // 现在我同时有贴膜和手机挂件了
            Sticker     sticker = new Sticker(phone);
            Accessories applePhoneWithAccessoriesAndSticker = new Accessories(sticker);

            applePhoneWithAccessoriesAndSticker.Print();

            Console.ReadLine();
        }
コード例 #33
0
ファイル: CoffeeStore.cs プロジェクト: RookieOne/Patterns
        public IBeverage GetCoffee(StoreType storeType, BeverageType beverageType)
        {
            IBeverage coffee = null;

            switch(storeType)
            {
                case StoreType.RegularStore :

                    switch (beverageType)
                    {
                        case BeverageType.HouseBlend:
                            coffee = new HouseBlend();
                            break;

                        case BeverageType.Roast:
                            coffee = new DarkRoast();
                            break;
                    }
                    break;

                case StoreType.ExoticStore:

                    switch (beverageType)
                    {
                        case BeverageType.HouseBlend:
                            coffee = new SpecialHouseBlend();
                            break;

                        case BeverageType.Roast:
                            coffee = new AmazonRoast();
                            break;
                    }
                    break;
            }

            return coffee;
        }
コード例 #34
0
ファイル: Program.cs プロジェクト: RookieOne/Patterns
        private static void WithDecorator()
        {
            IBeverage roast = new DarkRoast();

            Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice());
            Console.ReadLine();

            roast = new MilkDecorator(roast);

            Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice());
            Console.ReadLine();

            roast = new SugarDecorator(roast);

            Console.WriteLine("{0} cost {1}", roast.GetDescription(), roast.GetPrice());
            Console.ReadLine();

            IBeverage house = new HouseBlend();

            house = new MilkDecorator(house);

            Console.WriteLine("{0} cost {1}", house.GetDescription(), house.GetPrice());
            Console.ReadLine();
        }