1 / 6

Design Patterns

Design Patterns. A Pattern presents a proven advice in standard format A design pattern gives advice about a problem in software design. Attributes of s design Pattern: 1. Name 2. Context 3. Problem 4. Solution. What are iterators.

ash
Télécharger la présentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design Patterns • A Pattern presents a proven advice in standard format • A design pattern gives advice about a problem in software design. • Attributes of s design Pattern: 1. Name 2. Context 3. Problem 4. Solution

  2. What are iterators • Iterators helps to iterate or traverse the collections.[ From Start to End of collection] • Advantages : • Iterators does not expose internal structure [only return elements for use] • More than one iterators can be attached to a single collection.

  3. Iterators cont…. Consider a LinkedList LinkedList list = new LinkedList() How you will traverse in Java How you will traverse in C ListIterator Ltr = list.listIterator(); While(Ltr.hasNext() { Object current = Ltr.next(); ………………. } Link Ltr = list.head; While(Ltr != null) { Object current = Ltr.data; Ltr = Ltr.next; } Disadvantages • Internal structure links Exposed to user • Only one way traversal at a time

  4. The Iterator Pattern • Intent • Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation • An aggregate object is an object that contains other objects for the purpose of grouping those objects as a unit. It is also called a container or a collection Examples are a linked list and a hash table. • Also Known As Cursor • Motivation • An aggregate object such as a list should allow a way to traverse its elements without exposing its internal structure • It should allow different traversal methods • It should allow multiple traversals to be in progress concurrently

  5. Solution • Define an iterator that fetches one element at a time • Each iterator object keeps track of the position of the next element • If there are several aggregate/iterator variations, it is best if the • aggregate and iterator classes realize common interface types.

More Related