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));
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            var drawing = new GraphicObject {
                Name = "My drawing"
            };

            drawing.Children.Add(new Square {
                Color = "Red"
            });
            drawing.Children.Add(new Circle {
                Color = "Yellow"
            });

            var group = new GraphicObject();

            group.Children.Add(new Circle {
                Color = "Blue"
            });
            group.Children.Add(new Square {
                Color = "Blue"
            });
            drawing.Children.Add(group);

            Console.WriteLine(drawing);

            //// neuron and neuron layer
            var neuron1 = new Neuron();
            var neuron2 = new Neuron();

            neuron1.ConnectTo(neuron2);

            var layer1 = new NeuronLayer();
            var layer2 = new NeuronLayer();

            neuron1.ConnectTo(layer1);
            layer1.ConnectTo(layer2);

            // exercise
            IValueContainer sv = new SingleValue {
                Value = 1
            };
            IValueContainer mv = new ManyValues {
                2, 3,
            };
            var valueContainers = new List <IValueContainer> {
                sv, mv
            };

            Console.WriteLine(valueContainers.Sum());
        }
Ejemplo n.º 3
0
        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());
        }
Ejemplo n.º 4
0
        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());
        }