Data Structures and Algorithms
with Object-Oriented Design Patterns in C# |
Program introduces an abstract class called AbstractContainer. It is intended to be used as the base class from which concrete container realizations are derived. As illustrated in Figure , the AbstractContainer class extends the ComparableObject class (defined in Program ) and it implements the Container interface (defined in Program ).
A single field, count, is used. This field is used to keep track of the number of objects held in the container. The count field is initially zero by default. It is the responsibility of the derived class to update this field as required.
Program: AbstractContainer fields and properties.
The Count property provides a get accessor that returns the number of items contained in the container. The get accessor simply returns the value of the count field.
IsEmpty and IsFull bool-valued properties which indicate whether a given container is empty or full, respectively. Notice that the IsEmpty get accessor does not directly access the count field. Instead it uses Count property. As long as the Count property has the correct semantics, the IsEmpty property will too.
In some cases, a container is implemented in a way which makes its capacity finite. When this is the case, it is necessary to be able to determine when the container is full. IsFull is a bool-valued property that provides a get accessor that returns the value true if the container is full. The default version always returns false.