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

Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
Jafar Nesargi
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
Nitin Pai
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 

Tendances (20)

Java bean
Java beanJava bean
Java bean
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java features
Java  features Java  features
Java features
 
jQuery
jQueryjQuery
jQuery
 
Real data models of silicon valley
Real data models of silicon valleyReal data models of silicon valley
Real data models of silicon valley
 
Standard template library
Standard template libraryStandard template library
Standard template library
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
User Interface Analysis and Design
User Interface Analysis and DesignUser Interface Analysis and Design
User Interface Analysis and Design
 
OOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - LabOOAD - UML - Class and Object Diagrams - Lab
OOAD - UML - Class and Object Diagrams - Lab
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Date and Time Module in Python | Edureka
Date and Time Module in Python | EdurekaDate and Time Module in Python | Edureka
Date and Time Module in Python | Edureka
 
Software design
Software designSoftware design
Software design
 
JNDI
JNDIJNDI
JNDI
 
Generics
GenericsGenerics
Generics
 
Python sqlite3
Python sqlite3Python sqlite3
Python sqlite3
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
Design engineering
Design engineeringDesign engineering
Design engineering
 

En vedette (7)

The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
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
 
Command and Adapter Pattern
Command and Adapter PatternCommand and Adapter Pattern
Command and Adapter Pattern
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 

Similaire à Iterator Pattern

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 Pattern (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 Pattern

  • 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 }!