Download presentation
Presentation is loading. Please wait.
Published byKelly Stafford Modified over 9 years ago
1
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.
2
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.
3
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.
4
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 //... 17 }
5
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.
6
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 //.... 3 c.accept (new PrintingVisitor());
7
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 }
8
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 }
9
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//..
10
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.
11
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 }
12
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.
13
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}
14
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 }
15
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 } 21 22 public String toString () { 23 String result = "Association {" + key; 24 if (value != null) 25 result += ", " + value; 26 return result + "}"; 27 } 28 }
16
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
17
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?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.