Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.

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

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
The Singleton Pattern II Recursive Linked Structures.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration Pattern.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
1 Hashing Techniques: Implementation Implementing Hash Functions Implementing Hash Tables Implementing Chained Hash Tables Implementing Open Hash Tables.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
JAVA Objects & The Comparable Interface The MyComparable Interface. The MyComparable Interface. The AbstractObject class. The AbstractObject class. Wrapper.
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
1 Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Design Patterns 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Iterator Pattern.
1 Binary Tree Traversals Binary Tree Traversal classification Tree traversal animations DepthFirst traversal abstraction Accept method of AbstractTree.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
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.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
1 Lecture 09 Iterators and Enumerations Reading for these lectures: Weiss, Section Iterator Interface. Much better is: ProgramLive, Section 12.3.
U n i v e r s i t y o f H a i l ICS 202  2011 spring  Data Structures and Algorithms  1.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
 The general subject of comparing objects, or object references, can be introduced concretely with strings.  Recall that String is a class, and so strings.
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.
LinkedList Many slides from Horstmann modified by Dr V.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
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.
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Iterators.
Section 2.6 Linked List StringLog ADT Implementation
Computing with C# and the .NET Framework
Recap: Solution of Last Lecture Activity
Data Types and Abstraction
null, true, and false are also reserved.
Introduction to Design Patterns
Testing for Connectedness and Cycles
ArraySet Methods and ArrayIterator
Lecture 5 Stacks King Fahd University of Petroleum & Minerals
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Collections Framework
Introduction to Stacks
Introduction to Stacks
Ordered Structures Wellesley College CS230 Lecture 10
CSE 143 Lecture 21 Advanced List Implementation
Introduction to Design Patterns
Introduction to Design Patterns
Presentation transcript:

Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Association Pattern. The class Hierarchy. Review Questions.

What is Design Pattern? We've seen that inheritance allows related classes to share code, thus, allowing for code reusability, flexibility, etc. How about unrelated classes - can we make them reusable? Experience in the object-oriented design community has shown that interaction among even unrelated objects often takes the form of a recurring pattern or set of patterns. A number of these patterns have been identified and are being referred to as object-oriented design patterns. Learning to use these patterns makes code to become even more reusable, more flexible, etc. We shall use some of these patterns throughout the course and the basic ones are being introduced in this lecture.

The Container Pattern A container is an object that holds within it other objects. Many of the data structures we study in this course can be viewed as containers. i.e. they have the container pattern. The Container interface can be defined as below: The first four methods are obvious. We explain the other two after introducing Iterator and Visitor patterns. public interface Container { int getCount (); boolean isEmpty (); boolean isFull (); void purge (); void accept (Visitor visitor); Iterator iterator (); }

The AbstractContainer Class The following is the AbstractContainer class, that implements the Container interface and which will be used as base from which concrete container classes are derived. public abstract class AbstractContainer implements Container { protected int count; public int getCount () {return count;} public boolean isEmpty () {return getCount () == 0;} public boolean isFull () { return false; } public abstract void purge(); public void accept(Visitor v){... } public abstract Iterator iterator(); //... }

The Iterator Pattern The Iterator pattern provides a means to access one-by-one, all the objects in a container. The following shows the Iterator interface: An Iterator interacts closely with a container. Recall that a container has a method iterator, which returns an Iterator. The following shows how the Iterator interface is used: public interface Iterator { boolean hasNext (); Object next () throws NoSuchElementException; } Container c = new SomeContainer(); Iterator e = c.iterator(); while (e.hasNext()) System.out.println(e.next ());

The Visitor Pattern Many operations on data structures are observed to have the pattern of visiting each object - hence, the Visitor pattern. For this pattern we define the Visitor interface as follows: A visitor interacts closely with a container. The interaction goes as follows: –The container is passed a reference to a visitor by calling the container's accept method. –The container then calls the visit method of the visitor one-by-one for each object it contains. public interface Visitor { void visit (Object object); boolean isDone (); }

The Visitor Pattern (Contd.) The design framework for the accept method is as follows: The code for a sample visitor is shown below: Note: The loop that iterates the container’s objects is not in the visit method; It is in the container’s accept method. To print all objects in an instance, c of SomeContainer, the accept method is called as follows: public void accept(Visitor visitor) { for each object, obj, in this container visitor.visit(obj); } public class PrintingVisitor implements Visitor { public void visit(Object object) { System.out.println(object); } Container c = new SomeContainer(); //.... c.accept (new PrintingVisitor());

The isDone method The following shows the usefulness of the isDone method: public void accept (Visitor visitor){ for each Object o in this container if (visitor.isDone ()) return; visitor.visit (o); } public class MatchingVisitor implements Visitor { private Object target; private boolean found; public MatchingVisitor(Object target){ this.target = target; } public void visit (Object object) { if(object.equals (target)) found = true; } public boolean isDone (){return found;} }

The AbstractVisitor Class Many operations on a container involves visiting all the elements. i.e. they do not need to call the isDone method. Thus, forcing the implementation of the isDone method for such operations may not be desirable. To avoid this, we define the following AbstractVistor class. public abstract class AbstractVisitor implements Visitor { public abstract void visit (Object object); public boolean isDone () { return false; }

The toString Method The following defines the toString method for the AbstractContainer class using a visitor. Defining it here is aimed at simplifying the implementation of classes extending this class. public abstract class AbstractContainer implements Container { public String toString() { final StringBuffer buffer = new StringBuffer(); AbstractVisitor visitor = new AbstractVisitor() { private boolean comma; public void visit(Object obj) { if(comma) buffer.append( “, ” ); buffer.append(obj); comma = true; } }; accept(visitor); return "" + buffer; } //... }

The accept Method We now define the accept method for the AbstractContainer class using an iterator. public abstract class AbstractContainer implements Container { public void accept(Visitor visitor) { Iterator iterator = iterator(); while ( iterator.hasNext() && !visitor.isDone()) visitor.visit(iterator.next()); } //... } While the accept method takes only one visitor, a container can have more than one Iterator at the same time.

The SearchableContainer Pattern Some of the data structures we shall study have the additional property of being searchable. The SearchableContainer interface extends the Container interface by adding four more methods as shown below: The find method is used to locate an object in the container and returns its reference. It returns null if not found. public interface SearchableContainer extends Container { boolean isMember (Comparable object); void insert (Comparable object); void withdraw (Comparable obj); Comparable find (Comparable object); }

The Association Pattern An association is an ordered pair of objects. The first element is called the key, while the second is the value associated with the key. The following defines the Association class which we shall use whenever we need to associate one object to another. public class Association implements Comparable { protected Comparable key; protected Object value; public Association(Comparable comparable, Object obj){ key = comparable; value = obj; } public Association(Comparable comparable){ this(comparable, null); } //...

The Association Pattern (contd.) public Comparable getKey(){return key;} public void setKey(Comparable key){this.key = key;} public Object getValue(){return value;} public void setValue(Object value){this.value = value;} public int compareTo(Object obj){ Association association = (Association)obj; return key.compareTo(association.getKey()); } public boolean equals(Object obj){ return compareTo(obj) == 0; } public String toString() { String s = "{ " + key; if(value != null) s = s + ", " + value; return s + " }"; }

Review Question 1.Let c be an instance of some concrete class derived from the AbstractContainer class. Explain how the following statement prints the content of the container: System.out.println(c); 2.Suppose we have a container which contains only instances of the Integer class. Design a Visitor that computes the sum of all the integers in the container. 3.Using visitors, devise implementations for the isMember and find methods of the AbstractSearchableContainer class. Using visitors, devise implementations for the isMember and find methods of the AbstractSearchableContainer class. 4.Consider the following pair of Associations: Comparable a=new Association (new Integer(3), new Integer(4)); Comparable b=new Association (new Integer(3)); Give the sequence of methods called in order to evaluate a comparison such as " a.equals(b) ''. Is the result of the comparison true or false?