Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Behavioral Design Patterns May 5, 2015May 5, 2015May 5, 2015.
Behavioral Pattern: Template Method C h a p t e r 5 – P a g e 217 On occasion, two different components in a software system will have significant similarities,
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Copyright © 1995 –2004 Active Frameworks Inc. - All Rights Reserved - V2.0Behavioral Patterns - Page L8-1 PS95&96-MEF-L15-1 Dr. M.E. Fayad Creationa l.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Lecture 9 Concepts of Programming Languages
Iterator behavioural design pattern Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science (modified by Robin.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Behavioral Pattern: Memento C h a p t e r 5 – P a g e 179 Objects frequently expose only some of their internal state using public methods, but there.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Templates and the STL.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
13-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Abstract Factory Design Pattern making abstract things.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Chapter 9: The Iterator Pattern
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Lexi case study (Part 2) Presentation by Matt Deckard.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
131 3/30/98 CSE 143 More Collection ADTs [Sections ]
Object Oriented Programming in C++ Chapter 6 Inheritance.
Behavioral Pattern: Strategy C h a p t e r 5 – P a g e 205 The Open-Closed Principle advocates designing software in such a way that it will absorb new.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
CS212: Object Oriented Analysis and Design Lecture 37: Design Pattern.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Creational Pattern: Builder When a complex object needs to be created, it is sometimes beneficial to separate the construction of the object from its.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
1 The Standard Template Library Drozdek Section 3.7.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Programming Abstractions
More on Linked Lists, Stacks, and Queues
Stacks.
Stack and Queue APURBO DATTA.
Iterator and Composite Design Patterns
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 9 Behavioral Design Patterns
CSC 143 Queues [Chapter 7].
The iterator and memento patterns
CSC 143 Stacks [Chapter 6].
Design Patterns Difficult to describe abstractly Elements:
Queues Jyh-Shing Roger Jang (張智星)
Software Design Lecture : 39.
Standard Template Library
Chapter 3 Lists, Stacks, and Queues
Software Design Patterns
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Presentation transcript:

Behavioral Pattern: Iterator C h a p t e r 5 – P a g e 159 Software can become difficult to manage when a variety of different traversals of a variety of different data structures is needed. The Iterator Pattern accomplishes this without burdening the interfaces of the data structures with different traversal operations, while also providing a uniform interface for traversing various data structures (i.e., iterative polymorphism).

The Iterator Pattern C h a p t e r 5 – P a g e 160 The Iterator defines an interface for accessing and traversing elements of an Aggregate object. The ConcreteIterator implements the Iterator interface and keeps track of the current position in the traversal of the Aggregate. The Aggregate defines an interface for creating an Iterator object. The ConcreteAggregate implements the Iterator creation interface to return an instance of the proper ConcreteIterator.

C h a p t e r 5 – P a g e 161 Iterator Example: Web Scraper Web pages are separated into two categories: well-formed XML documents using tags to identify specific page elements (hyperlinks, images, etc.), and regular text pages using regular expressions to identify elements. The web scraper model contains concrete aggregate objects of both types, each of which has a pair of associate concrete iterators, one for collecting hyperlinks and one for collecting images. In this model, the Client has a uniform interface for harvesting different types of tags from websites.

C h a p t e r 5 – P a g e 162 Stack/Queue Iterator Code in C++ #include using namespace std; class ListAggregate { public: friend class Iter; virtual Iter *createIterator() const = 0; friend ostream& operator << (ostream &outputFile, const ListAggregate &la); }; class Iter { public: virtual void first() = 0; virtual void next() = 0; virtual bool isDone() = 0; virtual int currentItem() = 0; private: const ListAggregate *listAgg; int index; };

C h a p t e r 5 – P a g e 163 class Stack : public ListAggregate { public: friend class StackIter; Stack() { topIndex = -1; } void push(int in) { items[++topIndex] = in; } int pop() { return items[topIndex--]; } bool isEmpty() { return (topIndex == -1); } Iter *createIterator() const; private: int items[10]; int topIndex; }; class StackIter : public Iter { public: StackIter(const Stack *s) { stk = s; } void first() { index = stk->topIndex; } void next() { index--; } bool isDone() { return index == -1; } int currentItem() { return stk->items[index]; } private: const Stack *stk; int index; }; Iter *Stack::createIterator() const { return new StackIter(this); }

C h a p t e r 5 – P a g e 164 class Queue : public ListAggregate { public: friend class QueueIter; Queue() { rearIndex = -1; } void enqueue(int in) { items[++rearIndex] = in; } int dequeue() { int value = items[0]; for (int i = 0; i < rearIndex; i++) items[i] = items[i+1]; rearIndex--; return value; } bool isEmpty() { return (rearIndex == -1); } Iter *createIterator() const; private: int items[10]; int rearIndex; }; class QueueIter : public Iter { public: QueueIter(const Queue *q) { que = q; } void first() { index = 0; } void next() { index++; } bool isDone() { return index == que->rearIndex + 1; } int currentItem() { return que->items[index]; } private: const Queue *que; int index; }; Iter *Queue::createIterator() const { return new QueueIter(this); }

C h a p t e r 5 – P a g e 165 ostream& operator << (ostream &outputFile, const ListAggregate &la) { Iter *it = la.createIterator(); for (it->first(); !it->isDone(); it->next()) outputFile currentItem(); delete it; return outputFile; } bool operator == (const ListAggregate &l, const ListAggregate &r) { Iter *itl = l.createIterator(); Iter *itr = r.createIterator(); for (itl->first(), itr->first(); !itl->isDone(); itl->next(), itr->next()) if (itl->currentItem() != itr->currentItem()) break; bool ans = itl->isDone() && itr->isDone(); delete itl; delete itr; return ans; } void main() { Stack s1; Queue q1; int i; for (i = 1; i < 5; i++) s1.push(i); for (i = 4; i > 0; i--) q1.enqueue(i); Stack s2(s1), s3(s1), s4(s1); Queue q2(q1), q3(q1), q4(q1); s3.pop(); s4.push(7); q3.dequeue(); q4.enqueue(7);

C h a p t e r 5 – P a g e 166 cout << "Stacks: #1 (" << s1 << ") #2(" << s2 << ") #3 (" << s3 << ") #4 (" << s4 << ")\n"; cout << "Queues: #1 (" << q1 << ") #2(" << q2 << ") #3 (" << q3 << ") #4 (" << q4 << ")\n\n"; cout << "Are Stack 1 and Stack 1 equal? " << ((s1 == s1) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Stack 2 equal? " << ((s1 == s2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Stack 3 equal? " << ((s1 == s3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Stack 4 equal? " << ((s1 == s4) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Queue 1 equal? " << ((s1 == q1) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Queue 2 equal? " << ((s1 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Queue 3 equal? " << ((s1 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 1 and Queue 4 equal? " << ((s1 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Stack 2 and Stack 2 equal? " << ((s2 == s2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Stack 3 equal? " << ((s2 == s3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Stack 4 equal? " << ((s2 == s4) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Queue 1 equal? " << ((s2 == q1) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Queue 2 equal? " << ((s2 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Queue 3 equal? " << ((s2 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 2 and Queue 4 equal? " << ((s2 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Stack 3 and Stack 3 equal? " << ((s3 == s3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 3 and Stack 4 equal? " << ((s3 == s4) ? "YES!" : "NO! ") << endl; cout << "Are Stack 3 and Queue 1 equal? " << ((s3 == q1) ? "YES!" : "NO! ") << endl; cout << "Are Stack 3 and Queue 2 equal? " << ((s3 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 3 and Queue 3 equal? " << ((s3 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 3 and Queue 4 equal? " << ((s3 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Stack 4 and Stack 4 equal? " << ((s4 == s4) ? "YES!" : "NO! ") << endl; cout << "Are Stack 4 and Queue 1 equal? " << ((s4 == q1) ? "YES!" : "NO! ") << endl; cout << "Are Stack 4 and Queue 2 equal? " << ((s4 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Stack 4 and Queue 3 equal? " << ((s4 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Stack 4 and Queue 4 equal? " << ((s4 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Queue 1 and Queue 1 equal? " << ((q1 == q1) ? "YES!" : "NO! ") << endl; cout << "Are Queue 1 and Queue 2 equal? " << ((q1 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Queue 1 and Queue 3 equal? " << ((q1 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Queue 1 and Queue 4 equal? " << ((q1 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Queue 2 and Queue 2 equal? " << ((q2 == q2) ? "YES!" : "NO! ") << endl; cout << "Are Queue 2 and Queue 3 equal? " << ((q2 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Queue 2 and Queue 4 equal? " << ((q2 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Queue 3 and Queue 3 equal? " << ((q3 == q3) ? "YES!" : "NO! ") << endl; cout << "Are Queue 3 and Queue 4 equal? " << ((q3 == q4) ? "YES!" : "NO! ") << endl << endl; cout << "Are Queue 4 and Queue 4 equal? " << ((q4 == q4) ? "YES!" : "NO! ") << endl << endl; }

C h a p t e r 5 – P a g e 167

Iterator Pattern Advantages C h a p t e r 5 – P a g e 168 The Iterator Pattern provides ways to access elements of an aggregate object sequentially without exposing the underlying structure of the object. The Iterator abstraction is fundamental to an emerging technology called “generic programming”, which seeks to explicitly separate the notion of "algorithm" from that of "data structure". The motivation is to promote component- based development, to boost productivity, and to reduce configuration management. One drawback of iterators is that they give the illusion of order to unordered structures. For example, a set does not support ordering, and its Iterator would likely traverse the elements in an arbitrary sequence that could change over time. Program developers must be careful to produce code that does not assume consistency in the underlying structure.