[Specical] Design Pattern - Behavioral Patterns - Iterator Pattern
来源:互联网 发布:阿里云注册的域名解析 编辑:程序博客网 时间:2024/05/26 08:43
2007
Section 4, Chapter 3
Section 4, Chapter 3
Iterator Pattern
Concept
Iterators allow sequential access of elements in a collection of objects without exposing its underlying code.
Use
If you have a list object and you wish to move through each element in the list sequentially and maintain your current place in the list, an iterator seems appropriate.
Design
- Aggregate: a collection or aggregate object of some type whose elements we wish to iterate through.
- Iterator: a tool to move through the aggregate elements and keep track of the progress.
Illustration
class List{ private ArrayList _listItems = new ArrayList(); public Iterator CreateIterator() { return new Iterator(this); } public int Count { get{ return _listItems.Count; } } public void Append(object item) { _listItems.Add(item); } public void Remove(object item) { _listItems.Remove(item); } public void RemoveAt(int index) { _listItems.RemoveAt(index); } public object this[int index] { get{ return _listItems[index]; } set{ _listItems[index] = value; } }}class Iterator{ private List _collection; private int _currentIndex = 0; private int _stepCount = 1; public Iterator(List collection) { this._collection = collection; } public int Step { get{ return _stepCount; } set{ _stepCount = value; } } public object First() { _currentIndex = 0; return _collection[_currentIndex]; } public object Next() { _currentIndex += _stepCount; if(!IsDone()) return _collection[_currentIndex]; else return null; } public object Last() { _currentIndex = _collection.Count - 1; return _collection[_currentIndex]; } public object Current() { return _collection[_currentIndex]; } public bool IsDone() { return _currentIndex >= _collection.Count ? true : false ; }}
Iterator skipIterator = list.CreateIterator();skipIterator.Step = 3;
for(object item = skipIterator.First(); !skipIterator.IsDone(); item = skipIterator.Next())
0 0
- [Specical] Design Pattern - Behavioral Patterns - Iterator Pattern
- Iterator Pattern(GoF-Behavioral Design Patterns)
- Design Pattern----Behavioral Patterns
- Design Pattern - Behavioral Patterns - Observer Pattern
- Design Pattern - Behavioral Patterns - Mediator Pattern
- [Special] Design Pattern - Behavioral Patterns - Memento Pattern
- [Special] Design Pattern - Behavioral Patterns - Command Pattern
- [Special] Design Pattern - Behavioral Patterns - Interpreter Pattern
- [Special] Design Pattern - Behavioral Patterns - State Pattern
- Design Pattern - Behavioral Patterns - Strategy Pattern
- Design Pattern - Behavioral Patterns - Visitor Pattern
- Design Pattern - Behavioral Patterns - Template Pattern
- Command Pattern(GoF-Behavioral Design Patterns)
- Interpreter Pattern(GoF-Behavioral Design Patterns)
- Mediator Pattern(GoF-Behavioral Design Patterns)
- Memento Pattern(GoF-Behavioral Design Patterns)
- Design Pattern - Behavioral Patterns - Chain of Responsibility Pattern
- Template Method Pattern(Gof-Behavioral Design Pattern)
- iOS_逆向_使用usbmuxd进行ssh连接
- 前景检测算法(一)--综述
- WPF中的binding(十一)- Binding数据的转换
- 【译文】物联网:实施与挑战
- Android 一个欢迎页面应该干些什么?
- [Specical] Design Pattern - Behavioral Patterns - Iterator Pattern
- 31个与大数据有关的非常不错的资源和文章(附全链接)
- Lucene 学习(二):使用IK Analyzer中文分词
- activity启动模式
- 【译文】人工智能给伦敦带来了它的大脑和经济收益
- WPF中的binding(十二)- 多路Binding
- [开源].NET数据库访问框架Chloe.ORM
- 【投稿】Machine Learning With Spark Note 1:数据基本处理
- 前景检测算法(二)--codebook和平均背景法