Scenarios

Scenarios are a formal method of describing the sequence of events in a function that show the interaction of the function with other functions in the same or different classes.

Consider the problem of computing the cost of the grocery list in example 5, the process could be described as loop through the list and ask each item on the list for its cost. Add this cost to the total and return the total. Notice that this function asks each of the items in the list for its cost. If we had not listed a cost function as a member of GroceryItem, it would be clear here that we need such a function.

Scenarios formalize the process of cataloging functions called from a function. A scenario for our compute cost function is given below:

You will notice that the first item in each box is the class name while the second entry is the function name. At this point all that you seem to learn from the diagram is if other functions are needed in a this class or another. You will learn that if your function calls a function from a data item that is not a member of of your class, you will have to pass that data item as a parameter of your function. In more complex situations, this function call may actually be a message sent to other parts of your program.

From this scenario it becomes clear that you must have a way to loop through the list of GroceryItems retrieve each item and find out if you are at the end of a list. This introduces the concept of iterator.