Introduction To Design Patterns: 1 2. COSC 301: Data Structures
Introduction To Design Patterns: 1 2. COSC 301: Data Structures
• The first four methods are obvious. We explain the other two after
introducing Iterator and Visitor patterns.
COSC 301: Data Structures 2.3
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;
2.9
COSC 301: Data Structures
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.
accept(visitor);
return "" + buffer;
}
// ...
}
2.10
COSC 301: Data Structures
The accept Method
• While the accept method takes only one visitor, a container can have more
than one Iterator at the same time.
2.11
COSC 301: Data Structures
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:
2.12
COSC 301: Data Structures
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;
}