Data Structures and Algorithms with Object-Oriented Design Patterns in C#
next up previous contents index

Abstract Containers

Program gif 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 gif, the AbstractContainer class extends the ComparableObject class (defined in Program gif) and it implements the Container interface (defined in Program gif).

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.

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


next up previous contents index

Bruno Copyright © 2001 by Bruno R. Preiss, P.Eng. All rights reserved.