Categories
Uncategorized

User-friendly design Model – The iterative mode

Foreword

Today we take a look at patterns of behavior iterator pattern, iterative feedback process is repeated activities, its purpose is usually to approach and reach the desired goal or outcome. In the system development it can be understood as simply traverse. This mode is used for sequential access of elements of the set of objects, a set of objects not need to know the underlying or internal representation.

Iterator pattern Introduction

First, the reason

In the development of the system, showing an internal collection of different objects. It does not have the same underlying structure. For these objects, we hope at the same time its underlying internal representation and not exposed, which allows customers to access external elements. Iterator pattern provides a very elegant implementation for this demand.

Second, the intent

A method of providing a sequential access to each element in an aggregate object, but without exposing the interior of the object representation.

Third, the case of FIG.

 

Fourth, the iterative mode code sample

We can see from the chart above case, the iterator pattern mainly includes the following four parts:

Abstract iterator: defines the interface for accessing and traversing the element, and then implement these methods in its subclasses.

DETAILED iterator: The abstract iterator interface, complete traversal of the set of objects. While the position of the traverse track.

Abstract polymerization categories: mainly used to store objects, a corresponding iterator object created interface. With a createIterator () method is used to create iterator object.

Specific classes of polymerization: implementation creates the corresponding iterator object interface, createIterator () method, and returns to the particular polymerization ConcreteIterator instance corresponding specific iterator.

After introducing iterator pattern, then we look at the specific implement specific iterator pattern right. details as follows:

 

namespace Iterator_Pattern
{
    class IteratorPattern
    {
    }

    /// 
    /// 抽象聚合类、包含一个创建迭代器对象的方法
    /// 
    public interface IListAggregate
    {
        Iterator GetIterator();
    }

    /// 
    /// 抽象迭代器、包含访问和遍历元素的方法
    /// 
    public interface Iterator
    {
        /// 
        /// 是否有下一个元素
        /// 
        /// 
        bool IsNext();
        /// 
        /// 获取当前元素位置
        /// 
        /// 
        object GetCurrentIndex();
        /// 
        /// 获取下一个元素
        /// 
        void Next();
        /// 
        /// 获取第一个元素、相当于重置
        /// 
        void Start();
    }

    /// 
    /// 具体聚合类
    /// 
    public class ConcreteListAggregate : IListAggregate
    { 
        string[] list;
        public ConcreteListAggregate()
        {
            list = new string[] { "

Joe Smith

", "

John Doe

", "

Wang Wu

", "

Zhao six

" }; } /// /// 创建迭代器对象 /// /// public Iterator GetIterator() { return new ConcreteIterator(this); } /// /// 获取对象长度 /// public int Length { get { return list.Length; } } /// /// 获取指定位置元素 /// /// /// public object GetItem(int index) { return list[index]; } } public class ConcreteIterator : Iterator { private ConcreteListAggregate _list; private int _index; public ConcreteIterator(ConcreteListAggregate list) { _list = list; _index = 0; } public object GetCurrentIndex() { return _list.GetItem(_index); } public bool IsNext() { if (_index<_list.Length) { return true; } return false; } public void Next() { if (_index<_list.Length) { _index++; } } public void Start() { _index = 0; } } }

 

namespace Iterator_Pattern
{
    class Program
    {
        static void Main(string[] args)
        {
            //

Gets iterator object

IListAggregate listAggregate = new ConcreteListAggregate(); Iterator iterator = listAggregate.GetIterator(); while (iterator.IsNext()) { var result = iterator.GetCurrentIndex(); Console.WriteLine(result); iterator.Next(); } } } }

 

Usage scenarios and the advantages and disadvantages

First, the use scene

1, content access aggregate object does not need to expose the internal representation.

2, it is necessary to traverse a variety of ways to provide a polymeric object.

3, in order to traverse the different polymeric structure provides a unified interface objects

Second, the advantages

1, without exposing its internal representation polymerization access target content.

2, objects of different polymeric structure provides a uniform interface for the iterative mode.

3, on the same object can implement a variety of polymeric traversal.

4, adding new aggregate class-based and iterative more convenient, without modifying the code before

Third, shortcomings

1, the responsibility for the iterative mode through the data storage and data were isolated. Adding a new type of polymerization when the need to add new iterator class. In pairs increases. It increases the complexity of the system.

to sum up

Iterator pattern to introduce over here. Iterator pattern is a collection of objects traverse the region through the conduct separate iterator class. Internal Thus it may not be exposed collection of objects represents. But also allows the use of external normal access to its elements. This model is not complicated. A good grasp of where the responsibilities of each role, a coherent enough. In .Net we can also find a ready-made iterator pattern. This tutorial is the best case. IEnumerable polymerization as an abstract class, IEnumerator iterator as an abstract. Under the System.Collections namespace. Interested can go to get to the bottom of the case study.

 


Should a person can not make your life brilliant, but there is no reason to make it dark; life can be extraordinary, but not vulgar, fall; do not care how much life to plunder, but in the process of the pursuit of perfection and excellence!

C # Design Patterns series directory

Welcome to scan the next Fanger Wei code, and I set foot on the road to pass through the design patterns it!

  

 

Leave a Reply