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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
The Singleton Pattern II Recursive Linked Structures.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
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 
Queues What are queues? Queue Implementation Using Linked Lists. Applications of Queues.
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.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Introduction to Stacks What are Stacks? Stack implementation using arrays. Stack application.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
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.
Graph Traversals Depth-First Traversal. The Algorithm.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Binary Search Trees (BST) What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Binary Heaps What is a Binary Heap? Array representation of a Binary Heap MinHeap implementation Operations on MinHeap: Insert Delete Converting an array.
Review of Object-Oriented Concepts in JAVA Object-Oriented Concepts supported by JAVA. Object-Oriented Concepts supported by JAVA. Advantages of Object-Orientation.
Implementation of Graphs Identification of Classes. The Vertex Interface. The Edge Interface. Graph and Digraph Interfaces. Two concrete Implementations.
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.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Iterator Pattern Dr. Neal CIS 480. Iterator An iterator pattern can be used when one class is a collection of things and would like to provide a standardized.
Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Iterator Pattern. The Visitor Pattern. The SearchableContainer Pattern.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
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)
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
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.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
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.
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/23 Outline Creating Subclasses Overriding Methods Class Hierarchies.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
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 
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
CS61B L13 Lists (1)Garcia / Yelick Fall 2003 © UCB  Dan Garcia ( Kathy Yelick  (
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 
Collections Dwight Deugo Nesa Matic
C19: Collection Classes (don’t forget to look at all the online code examples)
Day 3: The Command and Visitor Patterns. Preliminaries The Java static type system uses simple rules to infer types for Java expressions. The inferred.
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 
Object-Oriented Concepts
Section 2.6 Linked List StringLog ADT Implementation
Computing with C# and the .NET Framework
Chapter 3: Using Methods, Classes, and Objects
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
Introduction to Stacks
Introduction to Stacks
Ordered Structures Wellesley College CS230 Lecture 10
Introduction to Design Patterns
Introduction to Design Patterns
Presentation transcript:

Introduction to Design Patterns What is Design Pattern? The Container Pattern. The Visitor Pattern. The SearchableContainer Pattern. The Enumeration 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. Thus, we define the following Container interface, which will be implemented by many of the data structures we study. 1 public interface Container extends MyComparable { 2 int getCount (); 3 boolean isEmpty (); 4 boolean isFull (); 5 void purge (); 6 void accept (Visitor visitor); 7 Enumeration getEnumeration (); 8 } The first four methods are obvious. We explain the other two after introducing Visitor and Enumeration patterns.

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. 1 public abstract class AbstractContainer extends 2 AbstractObject implements Container { 3 protected int count; 4 5 public int getCount () { 6 return count; 7 } 8 public boolean isEmpty () { 9 return getCount () == 0; 10 } 11 public boolean isFull () { 12 return false; 13 } 14 public abstract void purge(); 15 public abstract Enumeration getEnumeration(); 16 // }

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: 1 public interface Visitor { 2 void visit (Object object); 3 boolean isDone (); 4 } 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.

The Visitor Pattern (Contd.) The design framework for the accept method is as follows: 1 public class SomeContainer implements Container { 2 public void accept(Visitor visitor) { 3 for each object, o, in this container 4 visitor.visit(o); 5 } The code for a sample visitor is shown below: 1 public class PrintingVisitor implements Visitor { 2 public void visit(Object object) { 3 System.out.println(object); 4 } To print all objects in an instance, c of SomeContainer, the accept method is called as follows: 1 Container c = new SomeContainer(); 2 // c.accept (new PrintingVisitor());

The isDone method 1 public void accept (Visitor visitor){ 2 for each Object o in this container 3 if (visitor.isDone ()) 4 return; 5 visitor.visit (o); 6//... The following shows the usefulness of the isDone method: 1 public class MatchingVisitor implements Visitor { 2 private Object target; 3 private Object found; 4 public MatchingVisitor (Object target) { 5 this.target = target; 6 } 7 public void visit (Object object) { 8 if (!isDone () && object.equals (target)) 9 found = object; 10 } 11 public boolean isDone () { 12 return found != null; 13 } 14 }

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. 1 public abstract class AbstractVisitor implements Visitor 2 { 3 public abstract void visit (Object object); 4 5 public boolean isDone () { 6 return false; 7 } 8 }

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. 1 public abstract class AbstractContainer extends 2 AbstractObject implements Container { 3 public String toString() { 4 final StringBuffer buffer = new StringBuffer(); 5 AbstractVisitor visitor = new AbstractVisitor() { 6 private boolean comma; 7 public void visit(Object obj) { 8 if(comma) 9 buffer.append(", "); 10 buffer.append(obj); 11 comma = true; 12 } 13 }; 14 accept(visitor); 15 return getClass().getName()+" {" + buffer + "}"; 16} 17//..

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: 1 public interface SearchableContainer 2 extends Container 3 { 4 boolean isMember (MyComparable object); 5 void insert (MyComparable object); 6 void withdraw (MyComparable obj); 7 MyComparable find (MyComparable object); 8} The find method is used to locate an object in the container and returns its reference. It returns null if not found.

The AbstractSearchableContainer class The following introduces the AbstractSearchableContainer class. It will be used as a base from which concrete searchable containers are derived. 1 public abstract class AbstractSearchableContainer extends 2 AbstractContainer implements SearchableContainer 3 { 4 public abstract boolean isMember(MyComparable comparable); 5 public abstract void insert(MyComparable comparable); 6 public abstract void withdraw(MyComparable comparable); 7 public abstract MyComparable find(MyComparable comparable); 8 }

The Enumeration Patterns Like Visitor, an Enumeration pattern provides a means to access one-by-one, all the objects is a container. The following shows the Enumeration interface: 1 public interface Enumeration { 2 boolean hasMoreElements (); 3Object nextElement () throws NoSuchElementException; 4 An enumeration interacts closely with a container. Recall that a container has a method getEnumeration. The following shows how Enumeration interface is used: 1 Container c = new SomeContainer(); 3 Enumeration e = c.getEnumeration(); 4 while (e.hasMoreElements()) { 5System.out.println(e.nextElement()); While the accept method takes only one visitor, a container can have more than one Enumeration at the same time.

The accept Method We now define the accept method for the AbstractContainer class using an enumeration. 1 public abstract class AbstractContainer extends 2 AbstractObject implements Container { 3 4 public void accept(Visitor visitor) { 5 Enumeration enumeration = getEnumeration(); 6 while ( enumeration.hasMoreElements() && 7 !visitor.isDone()) 8visitor.visit(enumeration.nextElement())); 9 } 10 //.. 11}

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 two set of object. 1 public class Association extends AbstractObject { 2 protected myComparable key; 3 protected Object value; 4 5 public Association(myComparable key, Object value) { 6 this.key = key; 7 this.value = value; 8 } 9 public Association (MyComparable key) { 10 this (key, null); 11 }

The Association Pattern (contd.) 12 public MyComparable getKey () { 13 return key; 14 } 15 public Object getValue () { 16 return value; 17 } 17 protected int compareTo (MyComparable object) { 18 Association association = (Association) object; 19 return key.compare (association.getKey ()); 20 } public String toString () { 23 String result = "Association {" + key; 24 if (value != null) 25 result += ", " + value; 26 return result + "}"; 27 } 28 }

Summary We have introduced the idea behind design patterns and we have introduced the basic ones that we shall be using. Our class hierarchy so far is as shown in the figure below: AbstractContainer MyComparable AbstractObjectt Chr Int Dbl Str Association Container SearchableContainer AbstractSearchableContainer

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.Give an implementation for the accept method of the AbstractContainer class that uses an enumeration. 3.Suppose we have a container which contains only instances of the Int class. Design a Visitor that computes the sum of all the integers in the container. 4.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. 5.Consider the following pair of Associations: MyComparable a=new Association (new Int(3), new Integer(4)); MyComparable b=new Association (new Int(3)); Give the sequence of methods called in order to evaluate a comparison such as "a.isEQ(b)''. Is the result of the comparison true or false?