17
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.

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

Embed Size (px)

Citation preview

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

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.

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

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.

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

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.

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

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 extends2 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 }

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

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 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.

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

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());

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

The isDone method

1 public void accept (Visitor visitor){2 for each Object o in this container3 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 }

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

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 }

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

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 extends2 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 //..

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

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.

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

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 }

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

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 ();

3 Object 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()) {

5 System.out.println(e.nextElement());

• While the accept method takes only one visitor, a container can have more than one Enumeration at the same time.

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

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())

8 visitor.visit(enumeration.nextElement()));

9 }

10 //..

11 }

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

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 }

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

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 }

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

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 AbstractObjecttChr

Int

Dbl

Str

Association

Container

SearchableContainer

AbstractSearchableContainer

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

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?