internal static void ExecuteDecorator() { //Intent // • Attach additional responsibilities to an object dynamically. // Decorators provide a flexible alternative to subclassing for extending // functionality. // • Client - specified embellishment of a core object by recursively // wrapping it. // • Wrapping a gift, putting it in a box, and wrapping the box. //Problem // You want to add behavior or state to individual objects at run-time. // Inheritance is not feasible because it is static and applies to an entire // class. Beverage beverage = new Cream(new Cream(new Espresso())); Beverage beverage2 = new Mocha(new Milk(new Americano())); Console.WriteLine(String.Format($"Beverage: {beverage.GetName()}")); Console.WriteLine(String.Format($"Cost: {beverage.GetCost()}")); Console.WriteLine(String.Format($"Beverage: {beverage2.GetName()}")); Console.WriteLine(String.Format($"Cost: {beverage2.GetCost()}")); }
public decimal GetPrice(CoffeeSize size, Cream cream, Sugar sugar) { // Arrange // Act var result = _coffee.GetPrice(size, cream, sugar); // Assert return(result); }
public IActionResult Coffee() { var vm = new CoffeeVm(); ICreamer creamer = new Cream(); ISweetener sugar = new Sugar(); vm.CoffeeCup = new CoffeeCup(creamer, sugar); return(View(vm)); }
public void Run() { var coffee = new Coffee(); _output.WriteLine(coffee.ToString()); var sugar = new Sugar(coffee,2); _output.WriteLine(sugar.ToString()); var cream = new Cream(sugar,1); _output.WriteLine(cream.ToString()); }
public void CreamCostTest() { CoffeeMachine coffee_machine = new Coffee(); coffee_machine = new Cream(coffee_machine); int expected = 57; int actual; actual = coffee_machine.GetCost(); Assert.AreEqual(expected, actual); }
public void CreamNameTest() { CoffeeMachine coffee_machine = new Coffee(); coffee_machine = new Cream(coffee_machine); string expected = "Кофе, со сливками"; string actual; actual = coffee_machine.Name; Assert.AreEqual(expected, actual); }
static void Main(string[] args) { { var latte = new Latte(LatteType.Double); var cinnamon = new Cinnamon(latte); var lemon = new Lemon(cinnamon, 2); var iceCubes = new IceCubes(lemon, 2, IceCubeType.Dry); var beverage = new ChocolateCrumbs(iceCubes, 2); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var beverage = new ChocolateCrumbs( // Внешний слой: шоколадная крошка new IceCubes( // | под нею - кубики льда new Lemon( // | | еще ниже лимон new Cinnamon( // | | | слоем ниже - корица new Latte(LatteType.Standart)), // | | | в самом сердце - Латте 2), // | | 2 дольки лимона 2, IceCubeType.Dry), // | 2 кубика сухого льда 2); // 2 грамма шоколадной крошки Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var capuccino = new Cappuccino(CappuccinoType.Standart); var chocolateSlice = new ChocolateSlice(capuccino, 6); var liquor = new Liquor(chocolateSlice, LiquorType.Nut); var beverage = new Cream(liquor); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } { var milkshake = new Milkshake(MilkshakeType.Big); var chocolateSlice = new ChocolateSlice(milkshake, 4); var liquor = new Liquor(chocolateSlice, LiquorType.Chocolate); var beverage = new Cream(liquor); Console.WriteLine(beverage.GetDescription() + " costs " + beverage.GetCost()); } }
public override (MilkProduct mainProduct, List <MilkProduct> sideProducts) Transform(MilkProduct ingredients) { double valueNewProd = ingredients.Value / OutputRate; double milkPercetageNewProd = ingredients.Value * ingredients.MilkPercentage / valueNewProd; string productName = typeof(Cream).Name; var mainProduct = new Cream { Name = typeof(Cream).Name, Value = ingredients.Value / OutputRate, MilkPercentage = milkPercetageNewProd }; var sideProduct = new Obrat(); var sideProducts = new List <MilkProduct> { sideProduct }; var products = (mainProduct : mainProduct, sideProducts : sideProducts); return(products); }
static void Main(string[] args) { Console.WriteLine("-------Adapter DEMO:-------"); Console.WriteLine("Let's make standard cake:"); Cake cake = new Cake(); cake.RequestToMakeCake(); // Create adapter and place a request Console.WriteLine("Let's make special cake for our special allergic client:"); Cake allergicCake = new AdapterCake(); allergicCake.RequestToMakeCake(); Console.WriteLine(); Console.WriteLine("-------Bridge DEMO:-------"); // make birthday cake CakeForEvent cakeForEvent = new BirthdayCake(new CandyGift()); cakeForEvent.MakeEventCake(); Console.WriteLine(cakeForEvent.MakeEventCake() + " with gift-> " + cakeForEvent.AddGiftForEvent()); // make wedding cake cakeForEvent = new WeddingCake(new CandyGift()); cakeForEvent.Gift = new BisciutGift(); cakeForEvent.MakeEventCake(); Console.WriteLine(cakeForEvent.MakeEventCake() + " with gift-> " + cakeForEvent.AddGiftForEvent()); Console.WriteLine(); Console.WriteLine("-------Decorator DEMO:-------"); CreamCake napoleon = new Napoleon(); napoleon = new MilkCream(napoleon); Console.WriteLine("Name: {0}", napoleon.Name); Console.WriteLine("Price: {0}", napoleon.GetCost()); CreamCake kiev = new Kiev(); kiev = new ChocoCream(kiev); Console.WriteLine("Name: {0}", kiev.Name); Console.WriteLine("Price: {0}", kiev.GetCost()); CreamCake kievSpecial = new Kiev(); kievSpecial = new MilkCream(kievSpecial); kievSpecial = new ChocoCream(kievSpecial); Console.WriteLine("Name: {0}", kievSpecial.Name); Console.WriteLine("Price: {0}", kievSpecial.GetCost()); Console.WriteLine(); Console.WriteLine("-------Facade DEMO:-------"); Flakes flakes = new Flakes(); Cream cream = new Cream(); Pastry pastry = new Pastry(); PieMakerFacade pieMaker = new PieMakerFacade(cream, flakes, pastry); Console.WriteLine("Let's make our cake"); pieMaker.MakeCake(); Console.WriteLine("Let's decorate our cake"); pieMaker.DecorateCake(); Console.WriteLine(); Console.WriteLine("-------Flyweight DEMO:-------"); string[] flowers = { "RedFlower", "WhiteFlower", "WhiteFlower", "WhiteFlower" }; DecorationFlowerFactory factory = new DecorationFlowerFactory(); // For each flower use a flyweight object foreach (string flower in flowers) { DecorationFlower decorationFlower = factory.GetDecorationFlowers(flower); decorationFlower.DisplaySize(); } }
public Statistics <Cream> GetOrderedTogetherWithCream(int creamId) { try { Cream cream = _context.Cream.Where(x => x.Cream_Id == creamId).FirstOrDefault(); Statistics <Cream> result = new Statistics <Cream> { Object = cream, ObjectName = cream.Name, NumberOfOrders = _context.Order.Where(x => x.Cream_Id == creamId).Count() }; List <Cake> orderedCakes = _context.Order.Where(x => x.Cream_Id == creamId) .Include(x => x.Cake).Select(x => x.Cake).Distinct().ToList(); foreach (var cake in orderedCakes) { var totalAmount = _context.Order.Where(x => x.Cream_Id == creamId && x.Cake_Id == cake.Cake_Id).Count(); Statistics <Cake> statistics = new Statistics <Cake> { Object = cake, ObjectName = cake.Name, NumberOfOrders = totalAmount }; result.OrderedTogetherCakes.Add(statistics); } List <Additional> orderedAdditionals = _context.OrdersAdditionals.Where(x => x.Order.Cream_Id == creamId) .Include(x => x.Order).Include(x => x.Additional).Select(x => x.Additional).Distinct().ToList(); foreach (var additional in orderedAdditionals) { var totalAmount = _context.OrdersAdditionals.Where(x => x.Order.Cream_Id == creamId && x.Additional_Id == additional.Additional_Id) .Include(x => x.Order).Include(x => x.Additional).Count(); Statistics <Additional> statistics = new Statistics <Additional> { Object = additional, ObjectName = additional.Name, NumberOfOrders = totalAmount }; result.OrderedTogetherAdditionals.Add(statistics); } List <Decoration> orderedDecorations = _context.OrdersDecorations.Where(x => x.Order.Cream_Id == creamId) .Include(x => x.Order).Include(x => x.Decoration).Select(x => x.Decoration).Distinct().ToList(); foreach (var decoration in orderedDecorations) { var totalAmount = _context.OrdersDecorations.Where(x => x.Order.Cream_Id == creamId && x.Decoration_Id == decoration.Decoration_Id) .Include(x => x.Order).Include(x => x.Decoration).Count(); Statistics <Decoration> statistics = new Statistics <Decoration> { Object = decoration, ObjectName = decoration.Name, NumberOfOrders = totalAmount }; result.OrderedTogetherDecorations.Add(statistics); } return(result); } catch (Exception ex) { LoggerError.Logger.Error(ex); return(new Statistics <Cream>()); } }
public Dessert(string name, List <Ingredient> ingredients, Cream cream, Chocolate chocolate) : base(name) { Fruits = ingredients.OfType <Fruit>().ToList(); CreamType = cream; ChocolateType = chocolate; }
public Cream CreateCream(string name, string brand, decimal price, GenderType gender, ScentType scent) { Cream cream = new Cream(name, brand, price, gender, scent); return(cream); }