Feb 200692.3913 Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
Plab – Tirgul 12 Design Patterns
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Design Patterns. What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Chapter 19 Java Data Structures
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Sayed Ahmed Computer Engineering, BUET, Bangladesh Masters from the University of Manitoba, Canada
Beware of bugs in the above code; I have only proved it correct, not tried it.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
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.
CSC 480 Software Engineering Design With Patterns.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Chapter 7: The Adapter Pattern. Object Oriented Adapters Suppose that you have existing software. You have outsourced some of your work and there is a.
Religious Studies 313 – Advanced Programming Topics.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Computer Science 209 Software Development Inheritance and Composition.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
What is Iterator Category: Behavioral Generic Way to Traverse Collection Not Related to Collection Implementation Details Not Related to the direction/fashion.
The Factory Pattern Sanjay Yadav (ISE ).
CSC 480 Software Engineering Design With Patterns.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Chapter 19 Java Data Structures
Factory Patterns 1.
Software Design and Architecture
Factory Method Pattern
Software Engineering Lecture 7 - Design Patterns
Abstract Factory Pattern
Chapter 20 Lists, Stacks, Queues, and Priority Queues
CSE 143 Lecture 27: Advanced List Implementation
CSC 480 Software Engineering
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Graphical User Interfaces in Java Event-driven programming
Interator and Iterable
CSC 480 Software Engineering
Java Generics & Iterators
Presentation transcript:

Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The Collection interface defines a method: Iterator iterator() that returns an iterator Each subclass of Collection (LinkedList, Vector, …) implements iterator() differently. This allows the programmer to iterate through the objects of the collection without knowing what subclasses the iterator or collection objects belongs to. A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. We know the iterator has certain behaviour but we don’t know what subclass the iterator belongs to Note: we cover the Iterator design pattern in a later chapter

Feb Ron McFadyen2 Factory Method The Collection interface defines a method: Iterator iterator() that returns an iterator A statement such as Iterator iter = myCollection.iterator(); returns an iterator regardless of what type of collection object myCollection is. Without a framework like this, to instantiate an iterator we would need to know the iterator class and we would have to do : Iterator iter = new MyCollectionIterator?(myCollection); i.e. we need to know the class the iterator belongs to. Using the Factory Method, we don’t know the subclass for the iterator (and we don’t need to know)

Feb Ron McFadyen3 Iterator Example AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … iterator() is the factory method that returns an iterator object instantiated from the correct iterator subclass for the collection subclass ListIterator next() hasNext() remove() … If we extend the capability to handle a new collection we need to provide a collection subclass and an iterator subclass

Feb Ron McFadyen4 With collaboration shown AbstractCollection iterator() methodA() methodB() … AbstractIterator next() hasNext() remove() Vector iterator() … List iterator() … VectorIterator next() hasNext() remove() Client … ListIterator next() hasNext() remove() … FactoryMethod Abstract factory Concrete factory Concrete product Abstract product

Feb Ron McFadyen5 Generic Factory Method Creator factoryMethod() anOperation() Product next() hasNext() remove() ConcreteCreator factoryMethod() Client // typcially anOperation() is of the form: Public Product anOperation() Product prod = factoryMethod() Return prod ConcreteProduct()

Feb Ron McFadyen6 Behaviour concreteCreator client concreteProduct anOperation() factoryMethod() anotherOperation()

Feb Ron McFadyen7 Behaviour Exercise: show the behaviour for when the “dynamic” pizzaTestDrive orders a Chicago style cheese pizza from the time just after the store is instantiated until just after it obtains the name of the pizza. Exercise: show the behaviour for obtaining an iterator for a linked list up to and including getting the second object in the linked list.