public void TestCompositeSum() { var singleValue = new SingleValue { Value = 11 }; var otherValues = new ManyValues(); otherValues.Add(22); otherValues.Add(33); Assert.That(new List <IValueContainer> { singleValue, otherValues }.Sum(), Is.EqualTo(66)); }
private static void Main(string[] args) { /* * In this example we are using composition pattern to create various GraphicObject objects * which can represent a single graphic object entity like a colored square, or circle OR a group * of graphic objects which contains multiple GraphicObjects inside. */ var drawing = new GraphicObject("My Super Drawing") .AddChildren(new Rectangle(Color.Blue)) .AddChildren(new Square(Color.Green).AddChildren(new Rectangle(Color.Yellow))); var group = new GraphicObject() .AddChildren(new Square(Color.Red)) .AddChildren(new Square(Color.Red)) .AddChildren(new Rectangle(Color.Yellow)); drawing.AddChildren(group); WriteLine(drawing); /* * In this exercise we use composite pattern to implement an extension method * which operates on all List<IValueContainer> types. IValueContainer provides * the required composite interface for a SingleValue or ManyValues classes to * implement so that they can all be treated as IEnumerable<int> implementations. * This way, whether it is a SingleValue, or ManyValues this extension method * will be able to calculate the sum of all values because the implementation uses * composite pattern. */ var manyValues = new ManyValues(); manyValues.Add(10); manyValues.Add(20); manyValues.Add(5); var list = new List <IValueContainer> { new SingleValue { Value = 5 }, new SingleValue { Value = 5 }, manyValues }; WriteLine(list.Sum()); }
static void Main(string[] args) { var mv = new ManyValues(); var mv2 = new ManyValues(); mv.Add(1); mv.Add(2); mv.Add(3); mv.Add(4); mv.Add(5); mv2.Add(6); mv2.Add(7); mv2.Add(8); mv2.Add(9); mv2.Add(10); var iValueContainerList = new List <IValueContainer>() { mv, mv2 }; Console.WriteLine(iValueContainerList.Sum()); }