SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
Iterator	
  Design	
  Pa.ern	
  

      Stewart	
  Gleadow	
  
   University	
  of	
  Melbourne	
  
Intent	
  
•  “Provide	
  a	
  way	
  to	
  access	
  the	
  elements	
  of	
  an	
  
      aggregate	
  object	
  sequen>ally	
  without	
  
      exposing	
  its	
  underlying	
  representa>on”	
  
  	
   	
  	
   	
   	
   	
   	
   	
  –	
  GoF	
  
Mo3va3on	
  
•  Hide	
  implementa3on	
  details	
  

•  Allow	
  for	
  different	
  traversals	
  

•  Separate	
  traversal	
  code	
  from	
  the	
  list	
  itself	
  
Basic	
  Structure	
  
        List                                  Iterator
count()                                 first()
add(Item)                               next()
remove(Item)                            isDone()
Item get(int index)                     Item currentItem()




•  Provide	
  List	
  to	
  Iterator	
  (3ghtly	
  coupled)	
  

•  Iterator	
  keeps	
  a	
  Cursor	
  to	
  current	
  loca3on	
  
Structure	
  
     Aggregate                                  Iterator
 createIterator()                          first()
                                           next()
                                           isDone()
                                           Item currentItem()



ConcreteAggregate                           ConcreteIterator
 createIterator()



return new ConcreteIterator(this)
Types	
  of	
  Iterators	
  
•  External:	
  client	
  controls	
  the	
  itera3on	
  

•  Internal:	
  iterator	
  controls	
  itera3on	
  
        •  anonymous	
  func3ons	
  
        •  closures	
  /	
  blocks	
  
Applicability	
  
•  List	
  processing	
  &	
  selec3on	
  

•  Tree	
  traversals.	
  eg.	
  depth	
  first	
  

•  Syntax	
  trees	
  -­‐>	
  Interpreter?	
  
Concurrency	
  
•  hasNext()	
  or	
  !isDone()	
  implies	
  subsequent	
  call	
  
   to	
  currentItem()	
  will	
  pass	
  

•  Iterators	
  get	
  a	
  copy	
  of	
  the	
  data?	
  

•  Iterators	
  keep	
  track	
  of	
  modifica3ons?	
  
External	
  (Java)	
  
Iterator<MyType> iter = list.iterator();!
while(iter.hasNext())!
 !System.out.println(iter.next());!

// OR!

for(MyType item : list)!
 !System.out.println(item);!
External	
  (Ruby)	
  
for item in list!
 !puts item!
end!

// OR!

list.each { |item| puts item }!
Internal	
  (Java)	
  
public abstract class Internal<T> {!
 !private Iterator<T> iter;!

     public void traverse() {!
        for(T item : iter)!
          process(item);!
     }!

     protected abstract process(T item);!
}!
Internal	
  (Java)	
  
Iterator<T> iter = list.iterator();!
Internal<T> internal = new Internal<T>(iter)!
{!
 !protected void process(T item) {!
 ! ! !if(condition)!
 ! ! ! !System.out.println(item);!
 !}!
};!
Internal	
  (Ruby)	
  

list.each { |item| puts item if condition }!

Contenu connexe

Tendances

Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 

Tendances (20)

Java IO
Java IOJava IO
Java IO
 
Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Collections in Java Notes
Collections in Java NotesCollections in Java Notes
Collections in Java Notes
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
OOP Assignment 03.pdf
OOP Assignment 03.pdfOOP Assignment 03.pdf
OOP Assignment 03.pdf
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Vector class in C++
Vector class in C++Vector class in C++
Vector class in C++
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Files in java
Files in javaFiles in java
Files in java
 
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
Java Collections | Collections Framework in Java | Java Tutorial For Beginner...
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | EdurekaWhat is Dictionary In Python? Python Dictionary Tutorial | Edureka
What is Dictionary In Python? Python Dictionary Tutorial | Edureka
 

En vedette

2 Slides Conditionals Iterators Blocks Hashes Arrays
2 Slides   Conditionals Iterators Blocks Hashes Arrays2 Slides   Conditionals Iterators Blocks Hashes Arrays
2 Slides Conditionals Iterators Blocks Hashes Arrays
liahhansen
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
sandeep kumar
 
iterators-must-go
iterators-must-goiterators-must-go
iterators-must-go
Hiroshi Ono
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
melbournepatterns
 

En vedette (13)

Iterator Pattern
Iterator PatternIterator Pattern
Iterator Pattern
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Ruby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus IteratorsRuby Made Simple: Blocks Plus Iterators
Ruby Made Simple: Blocks Plus Iterators
 
2 Slides Conditionals Iterators Blocks Hashes Arrays
2 Slides   Conditionals Iterators Blocks Hashes Arrays2 Slides   Conditionals Iterators Blocks Hashes Arrays
2 Slides Conditionals Iterators Blocks Hashes Arrays
 
Ruby iterators
Ruby iteratorsRuby iterators
Ruby iterators
 
Blocks and loops.pptx
Blocks and loops.pptxBlocks and loops.pptx
Blocks and loops.pptx
 
iterators-must-go
iterators-must-goiterators-must-go
iterators-must-go
 
C# Strings
C# StringsC# Strings
C# Strings
 
Behavioral Design Patterns
Behavioral Design PatternsBehavioral Design Patterns
Behavioral Design Patterns
 
Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621Iterator Pattern Baljeet Sandhu 20060621
Iterator Pattern Baljeet Sandhu 20060621
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 

Similaire à Iterator

Java class 5
Java class 5Java class 5
Java class 5
Edureka!
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 

Similaire à Iterator (20)

Java class 5
Java class 5Java class 5
Java class 5
 
Iterarators and generators in python
Iterarators and generators in pythonIterarators and generators in python
Iterarators and generators in python
 
Collections
CollectionsCollections
Collections
 
More topics on Java
More topics on JavaMore topics on Java
More topics on Java
 
Java Tutorials
Java Tutorials Java Tutorials
Java Tutorials
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
 
Kotlin 1.2: Sharing code between platforms
Kotlin 1.2: Sharing code between platformsKotlin 1.2: Sharing code between platforms
Kotlin 1.2: Sharing code between platforms
 
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ....NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
.NET Fest 2018. Дмитрий Иванов. Иммутабельные структуры данных в .NET: зачем ...
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Java Advanced Features
Java Advanced FeaturesJava Advanced Features
Java Advanced Features
 
Data structures
Data structuresData structures
Data structures
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Python internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvandPython internals and how they affect your code - kasra ahmadvand
Python internals and how they affect your code - kasra ahmadvand
 
java training faridabad
java training faridabadjava training faridabad
java training faridabad
 
Pythonintroduction
PythonintroductionPythonintroduction
Pythonintroduction
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Object Oriented Programming Concepts using Java
Object Oriented Programming Concepts using JavaObject Oriented Programming Concepts using Java
Object Oriented Programming Concepts using Java
 
Clojure Intro
Clojure IntroClojure Intro
Clojure Intro
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 

Plus de melbournepatterns

Abstract Factory Design Pattern
Abstract Factory Design PatternAbstract Factory Design Pattern
Abstract Factory Design Pattern
melbournepatterns
 

Plus de melbournepatterns (20)

An Introduction to
An Introduction to An Introduction to
An Introduction to
 
State Pattern from GoF
State Pattern from GoFState Pattern from GoF
State Pattern from GoF
 
Concurrency Patterns
Concurrency PatternsConcurrency Patterns
Concurrency Patterns
 
Continuous Integration, Fast Builds and Flot
Continuous Integration, Fast Builds and FlotContinuous Integration, Fast Builds and Flot
Continuous Integration, Fast Builds and Flot
 
Command Pattern
Command PatternCommand Pattern
Command Pattern
 
Code Contracts API In .Net
Code Contracts API In .NetCode Contracts API In .Net
Code Contracts API In .Net
 
LINQ/PLINQ
LINQ/PLINQLINQ/PLINQ
LINQ/PLINQ
 
Gpu Cuda
Gpu CudaGpu Cuda
Gpu Cuda
 
Facade Pattern
Facade PatternFacade Pattern
Facade Pattern
 
Phani Kumar - Decorator Pattern
Phani Kumar - Decorator PatternPhani Kumar - Decorator Pattern
Phani Kumar - Decorator Pattern
 
Composite Pattern
Composite PatternComposite Pattern
Composite Pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Prototype Design Pattern
Prototype Design PatternPrototype Design Pattern
Prototype Design Pattern
 
Factory Method Design Pattern
Factory Method Design PatternFactory Method Design Pattern
Factory Method Design Pattern
 
Abstract Factory Design Pattern
Abstract Factory Design PatternAbstract Factory Design Pattern
Abstract Factory Design Pattern
 
A Little Lisp
A Little LispA Little Lisp
A Little Lisp
 
State Pattern in Flex
State Pattern in FlexState Pattern in Flex
State Pattern in Flex
 
Active Object
Active ObjectActive Object
Active Object
 
Extract Composite Talk Andy
Extract Composite Talk AndyExtract Composite Talk Andy
Extract Composite Talk Andy
 
Selenium Interpreter
Selenium InterpreterSelenium Interpreter
Selenium Interpreter
 

Iterator

  • 1. Iterator  Design  Pa.ern   Stewart  Gleadow   University  of  Melbourne  
  • 2. Intent   •  “Provide  a  way  to  access  the  elements  of  an   aggregate  object  sequen>ally  without   exposing  its  underlying  representa>on”                  –  GoF  
  • 3. Mo3va3on   •  Hide  implementa3on  details   •  Allow  for  different  traversals   •  Separate  traversal  code  from  the  list  itself  
  • 4. Basic  Structure   List Iterator count() first() add(Item) next() remove(Item) isDone() Item get(int index) Item currentItem() •  Provide  List  to  Iterator  (3ghtly  coupled)   •  Iterator  keeps  a  Cursor  to  current  loca3on  
  • 5. Structure   Aggregate Iterator createIterator() first() next() isDone() Item currentItem() ConcreteAggregate ConcreteIterator createIterator() return new ConcreteIterator(this)
  • 6. Types  of  Iterators   •  External:  client  controls  the  itera3on   •  Internal:  iterator  controls  itera3on   •  anonymous  func3ons   •  closures  /  blocks  
  • 7. Applicability   •  List  processing  &  selec3on   •  Tree  traversals.  eg.  depth  first   •  Syntax  trees  -­‐>  Interpreter?  
  • 8. Concurrency   •  hasNext()  or  !isDone()  implies  subsequent  call   to  currentItem()  will  pass   •  Iterators  get  a  copy  of  the  data?   •  Iterators  keep  track  of  modifica3ons?  
  • 9. External  (Java)   Iterator<MyType> iter = list.iterator();! while(iter.hasNext())! !System.out.println(iter.next());! // OR! for(MyType item : list)! !System.out.println(item);!
  • 10. External  (Ruby)   for item in list! !puts item! end! // OR! list.each { |item| puts item }!
  • 11. Internal  (Java)   public abstract class Internal<T> {! !private Iterator<T> iter;! public void traverse() {! for(T item : iter)! process(item);! }! protected abstract process(T item);! }!
  • 12. Internal  (Java)   Iterator<T> iter = list.iterator();! Internal<T> internal = new Internal<T>(iter)! {! !protected void process(T item) {! ! ! !if(condition)! ! ! ! !System.out.println(item);! !}! };!
  • 13. Internal  (Ruby)   list.each { |item| puts item if condition }!