Software Design Lecture : 39.

Slides:



Advertisements
Similar presentations
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Advertisements

Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Chapter 9: The Iterator Pattern
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Generic Programming and Inner classes ge·ner·ic 1a : relating or applied to or descriptive of all members of a genus, species, class, or group : common.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Iteration Abstraction SWE Software Construction Fall 2009.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Interpreter By: Mahmoodreza Jahanseir Amirkabir University of Technology Computer Engineering Department Fall 2010.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
Examples (D. Schmidt et al)
Iterators.
Design Patterns: MORE Examples
Andy Wang Object Oriented Programming in C++ COP 3330
Programming with ANSI C ++
Object-Oriented Analysis and Design
Chapter 10 Design Patterns.
Chapter 19 Java Data Structures
Introduction to Design Patterns
Data Structure and Algorithms
Sequences and Iterators
Chapter 12: Data Structures
Lecture 2 of Computer Science II
Iterator Design Pattern
Data Structures and Database Applications Abstract Data Types
Iteration Abstraction
CS313D: Advanced Programming Language
Advanced Programming Behnam Hatami Fall 2017.
Component-Level Design
Chapter 13 Collections.
Object Oriented Design Patterns - Behavioral Patterns
Menu item at a restaurant
Behavioral Design Pattern
Dynamic Data Structures and Generics
Object-Oriented Programming (OOP) Lecture No. 40
Iteration Abstraction
The iterator and memento patterns
Interpreter Pattern.
CSE 214 – Computer Science I More on Linked Lists
Design Patterns Difficult to describe abstractly Elements:
Introduction to Design Patterns
Introduction to Data Structure
CIS 199 Final Review.
5. 3 Coding with Denotations
Software Design Lecture : 28.
Software Design Lecture : 36.
Iterator Design Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014
Lecture 3 – Data collection List ADT
Presentation transcript:

Software Design Lecture : 39

Category 3: Behavioral Patterns This category of patterns are concerned with algorithms and assignments of responsibilities between objects. They describe not just patterns of objects or classes but also the pattern of communication between them.

Behavioral Design Patterns This category will shift the focus from flow of control to concentrate just on the way objects are interconnected. These patterns are concerned with the assignment of responsibilities between objects, or, encapsulating behavior in an object and delegating requests to it. 

Iterator Design Pattern OR Well Managed Collection

Motivation for Iterator Design Pattern There are different data structure to store data or objects in a collection i-e Array, Array list , Vector, stack, link list etc. In other words there are different container which contain different object and to access the content of each container different mechanism is applied.

Array Vs Vector Arrays are Static data structure Vectors are dynamic data structure Arrays are data type specific Vector contains a dynamic list of references to other objects Since size is fixed memory is allocated for array Due to dynamic behavior Vectors are memory efficient way of storing data

Internal Iterator - Cursor The collection itself offers methods to allow a client to visit different objects within the collection. For example, the java.util.ResultSet class contains the data and also offers methods such as next() to navigate through the item list. There can be only one iterator on a collection at any given time. The collection has to maintain or save the state of iteration.

External Iterators The iteration functionality is separated from the collection and kept inside a different object referred to as an iterator Typical example include Vector in which elements are stored inside vector but are traversed using Enumeration for instance as shown in the example in next slide.

import java.util.*; public class Program { public static void main(String[] args) { Vector v = new Vector(); v.add(0, "Apple"); v.add(1, "Orange"); v.add(2, "Banana"); //Assigning Vector to Iterator Enumeration vEnum = v.elements(); while (vEnum.hasMoreElements()) System.out.println(vEnum.nextElement()); }}}

Output of the Program Apple Orange Banana

Iterator Pattern Defined “The Iterator Design pattern provides a way to access the element of aggregate object sequentially without knowing it’s underlying representation”

Intend of Iterator Design Pattern Allows a client object to access the contents of a container in a sequential manner, without having any knowledge about the internal representation of its contents. Client should not be involved in the internal traversal of the contents of the container.

Discussion on Iterator Design Pattern Uniform method of accessing elements of collection without knowing the underlying representation. We can write polymorphic code to access the elements of underlying aggregate objects.

Key idea behind Iterator The idea is to take out the responsibility for access and traversal out of the collection and put them in the Iterator Object. Iterator class will define an interface for accessing the element of the collection.

Iterator pattern The abstraction provided by the Iterator pattern allows you to modify the collection implementation without making any changes outside of collection. It enables you to create a general purpose GUI component that will be able to iterate through any collection of the application. 

Classes in Iterator Pattern The Iterator pattern suggests that a Container object should be designed to provide a public interface in the form of an Iterator object for different client objects to access its contents. An Iterator object contains public methods to allow a client object to navigate through the list of objects within the container

Class Diagram of Iterator Design Pattern

Consequences Benefits Simplifies the interface of the Aggregate by not polluting it with traversal methods Supports multiple, concurrent traversals Supports variant traversal techniques Liabilities None!

Example  This example is using a collection of books and it uses an iterator to iterate through the collection.

Class Diagram

interface IIterator { public boolean hasNext(); public Object next(); } interface IContainer { public IIterator createIterator(); }

class BooksCollection implements IContainer { private String m_titles[] = {"Design Patterns","1","2","3","4"}; public IIterator createIterator() { BookIterator result = new BookIterator(); return result; }

private class BookIterator implements IIterator { private int m_position; public boolean hasNext() { if (m_position < m_titles.length) return true; else return false; } public Object next() { if (this.hasNext()) return m_titles[m_position++]; else return null; }}}