Data Structures and Algorithms
with Object-Oriented Design Patterns in C#|
A container is an object that contains within it other objects. Many of the data structures presented in this book can be viewed as containers. For this reason, we develop a common interface that is implemented by the various data structure classes.
The Container interface is declared in Program . It comprises the three properties, Count, IsEmpty, IsFull, and two methods, Purge and Accept. In addition, the Container interface extends the IComparable interface (and therefore provides a CompareTo method) and it extends the IEnumerable interface (and therefore provides a GetEnumerator method).
Program: Container interface.
A container may be empty or it may contain one or more other objects. Typically, a container has finite capacity. The IsEmpty property provides a get accessor that returns true when the container is empty and the IsFull property provides a get accessor that returns true when the container is full. The Count property provides a get accessor that returns the number of objects in the container.
The purpose of the Purge method is to discard all of the contents of a container. After a container is purged, the IsEmpty property is true and the Count property is zero.
Conspicuous by their absence from Program are methods for putting objects into a container and for taking them out again. These methods have been omitted from the Container interface, because the precise nature of these methods depends on the type of container implemented.
In order to describe the remaining Accept methods we need to introduce first the concepts of a visitor and an enumerator , as well as with the Visitor, IEnumerable and IEnumerator interfaces which represent these concepts. Visitors are discussed below in Section and enumerators are discussed in Section .