예제 #1
0
        public static void Main()
        {
            Console.WriteLine(PrintMessages.CustomIterator);
            ConcreteAggregate <int> collection = new ConcreteAggregate <int>();

            collection.Add(1);
            collection.Add(2);
            collection.Add(3);
            collection.Add(4);
            collection.Add(5);

            IIterator <int> iterator = collection.CreateIterator();

            while (iterator.Next())
            {
                int item = iterator.Current;
                Console.WriteLine(item);
            }

            Console.WriteLine(new string('-', 50));

            Console.WriteLine(PrintMessages.NETIterator);
            IEnumerable <int> elements = new List <int>()
            {
                1, 2, 3, 4, 5
            };

            IEnumerator <int> enumerator = elements.GetEnumerator();

            while (enumerator.MoveNext())
            {
                int currentElement = enumerator.Current;
                Console.WriteLine(currentElement);
            }
        }
예제 #2
0
        static void IteratorExample()
        {
            var aggregate = new ConcreteAggregate();

            aggregate.Add(3);
            aggregate.Add(5);
            aggregate.Add(2);

            var iterator = aggregate.CreateIterator();

            for (object current = iterator.First(); !iterator.IsDone(); current = iterator.Next())
            {
                Console.WriteLine(current);
            }
        }
예제 #3
0
        public void TestIterator()
        {
            decimal expected =
                this._shopcart[0].UnitPrice * this._shopcart[0].Amount * 0.8M +
                this._shopcart[2].UnitPrice * this._shopcart[2].Amount;
            decimal actual = 0;

            Aggregate aggregate = new ConcreteAggregate(ProductTypeEnum.Book);

            this._shopcart.ForEach(prod =>
            {
                aggregate.Add(prod);
            });

            #region 改寫Visitor單元測試

            IObjectStructure checkout = new ObjectStructure();

            //Attach the elements into ObjectStructure
            checkout.Elements = aggregate.GetAll();

            //Accept all the elements and execute the strategy from certain Visitor
            checkout.Accept(new VisitorDiscount4Count());

            actual = checkout.Elements.Sum(x => x.TotalPrice);
            Assert.Equal(expected, actual);
            #endregion
        }
예제 #4
0
 private void LoadAgg(ConcreteAggregate agg)
 {
     agg.Add("Verizon : Hayden");
     agg.Add("Verizon : William");
     agg.Add("AT&T : Matt");
     agg.Add("AT&T : Frank");
     agg.Add("Sprint : Shawn");
     agg.Add("T-Mobile : Kat");
 }
예제 #5
0
        private void PopulateAgg(ConcreteAggregate aggregate)
        {
            aggregate.Add("Ben");
            aggregate.Add("Abbie");
            aggregate.Add("Anna");
            aggregate.Add("Berry");

            aggregate.Add("Balthasar");
            aggregate.Add("Carol");

            aggregate.Add("Camren");
            aggregate.Add("Cori");
            aggregate.Add("Casandra");
            aggregate.Add("David");
            aggregate.Add("Aaron");
            aggregate.Add("Dylan");
        }