The Data Collection Bag

In this lesson, we will describe the bag as a data collection, along with its behaviors.

We'll cover the following

The bag

Imagine a paper bag, a reusable cloth bag, or even a plastic bag. People use bags when they shop, pack lunch, or eat potato chips. Bags contain things. In everyday language, a bag is a kind of container. In Java, however, a container is an object whose class extends the standard class Container. Such containers are used in graphics programs. Rather than being considered a container, a bag in Java is a kind of collection.

What distinguishes a bag from other collections? A bag doesn’t do much more than contain its items. It doesn’t order them in a particular way, nor does it prevent duplicate items. Most of its behaviors are simple and could be performed by other kinds of collections.

While describing the behaviors that we want for the bag we’ll design in this chapter, let’s keep in mind that we are specifying a programming abstraction inspired by an actual physical bag. For example:

  • The capacity of a physical bag is limited by its size and shape, so we will limit the capacity of an abstract bag by the amount of memory assigned to its implementation
  • Since our abstract bag will hold objects, this limit is conveniently expressed as the maximum number of objects

Unless clearly stated otherwise, from now on when we refer to a bag we will mean a Java object representing an abstract bag.

A bag’s behaviors

Since a bag has an upper limit on the number of objects it can contain, we’ll specify this capacity when we create the bag. Reporting this capacity could be one of a bag’s behaviors:

  • Get the bag’s capacity

Likewise, we should be able to see how many objects are in a bag:

  • Get the number of items currently in the bag

Two related behaviors detect whether a bag is full or empty:

  • See whether the bag is full
  • See whether the bag is empty

We should be able to add and remove objects:

  • Add a given object to the bag
  • Remove an unspecified object from the bag
  • Remove an occurrence of a particular object from the bag
  • Remove all objects from the bag

While we hope that the bagger at the grocery store does not toss six cans of soup into a bag on top of our bread and eggs, our add operation does not indicate where in the bag an object should go. Remember that a bag does not order its contents.

Likewise, the first remove operation just removes any object it can. This operation is like reaching into a grab bag and pulling something out. On the other hand, the second remove operation looks for a particular item in the bag. If we find it, we take it out. If the bag contains several equal objects that satisfy our search, we remove any one of them. If we can’t find the object in the bag, we can’t remove it, and we just say so. Finally, the last remove operation simply empties the bag of all objects.

How many cans of dog food did you buy? Did you remember to get anchovy paste? Just what is in that bag? The following operations answer these questions:

  • Count the number of times a particular object occurs in the bag
  • Test whether the bag contains a particular object
  • Look at all objects in the bag

We have enough behaviors for now. At this point, we would have written all 11 behaviors down.

What type of objects will our bag contain? We might be tempted to accept all types of objects, much like a paper bag can hold things of various dimensions and shapes. However, we will restrict all objects in our bag to have the same data type. Let’s use String as this type and define a bag of strings. We have begun a design for a class we’ll name BagOfStrings.

A CRC card

A simple technique to represent a class at this early stage of the design involves using an index card. We write the class’s name at the top of the card. We then list the behaviors or responsibilities of the class. Finally, we write the names of any other classes that have some sort of interaction with our new class. These interactions are called collaborations. In our case, BagOfStrings collaborates with the class String.

Because of their content, these index cards are called class-responsibility-collaboration, or CRC, cards. The figure given below illustrates a CRC card for the class BagOfStrings.

Get hands-on with 1200+ tech skills courses.