public void Composite_pattern_test() { var colas = new Cola(210); colas.Flavors.Add(new VanillaCola(215)); colas.Flavors.Add(new CherryCola(220)); //var lemonLime = new LemonLime(185); //var rootBeers = new RootBeer(195); //rootBeers.Flavors.Add(new VanillaRootBeer(200)); //rootBeers.Flavors.Add(new StrawberryRootBeer(200)); SodaWater sodaWater = new SodaWater(180); sodaWater.Flavors.Add(colas); //sodaWater.Flavors.Add(lemonLime); //sodaWater.Flavors.Add(rootBeers); var actValue = sodaWater.DisplayCalories(); Assert.AreEqual("SodaWater: 180 caloriesCola: 210 caloriesVanillaCola: 215 caloriesCherryCola: 220 calories", actValue); }
static void Main(string[] args) { #region Soda var colas = new Cola(210); colas.Flavours.Add(new VanillaCola(215)); colas.Flavours.Add(new CherryCola(210)); var lemonLime = new LemonLime(185); var rootBeers = new RootBeer(195); rootBeers.Flavours.Add(new VanillaRootBeer(200)); rootBeers.Flavours.Add(new StrawberryRootBeer(200)); SodaWater sodaWater = new SodaWater(180); sodaWater.Flavours.Add(colas); sodaWater.Flavours.Add(lemonLime); sodaWater.Flavours.Add(rootBeers); sodaWater.DisplayCalories(0); #endregion #region Family // Parent 1 Person john = new Person("John"); // Parent 2 Person jane = new Person("Jane"); // Child 1 Person alice = new Person("Alice"); // Child 2 Person billy = new Person("Billy"); // Child 3 Person christine = new Person("Christine"); // John and Jane are both parents of Alice john.AddChild(alice); jane.AddChild(alice); // John is also Billy's parent john.AddChild(billy); // Jane is also Christine's parent jane.AddChild(christine); // Since David is John's brother he is also an uncle. Uncle david = new Uncle("David"); // David and John are both the children of their father Edward. Person edward = new Person("Edward"); edward.AddChild(john); // Even though 'david' is class of Uncle it can still be added // as a child. edward.AddChild(david); #endregion Console.ReadKey(); }