patpart4.htm

IV. Class Design Patterns


The verbs one encounters in problem narratives often are strong indicators of specific knowledge and/or behavioral responsibilities of the classes involved in the problem. In this section we study examples of these verbs and how they can help in designing classes. Chapter 5, Section III of The Story of C++ ( especially Parts 'C' and 'D') also discusses this. This section concludes with a discussion of one approach to implementing a list as a class-based data structure.

Pattern A: Verbs That Indicate Properties

Sentences with the verbs listed below often have a class as the subject and a property of that class as a object:

keep track of,       save,       store
hold,                    has/have

For example, the sentence:

Persons have names, ages, and grades

might well indicate that there is a class 'person' with the properties 'name', age', and 'grade'.

Pattern B. Verbs that Indicate the Need for Function Members That Change Property Values

Sentences with the verbs below indicate specific properties and the need for member functions (methods) that provide a way to change the property values:

change,         set,         modify

For example, the sentence:

One should be able to change the age of a person

might well indicate that there is a property called 'age' (of a 'person' class) and that a member function must exist to enable this property to be changed.

Such 'Change' member functions all have a similar design pattern:

    Purpose:   To change the value of the square footage data member
    Goal:        The square footage data member has a new value

    Receives: The new square footage (an integer)
    Returns:   NONE


Pattern C. Verbs that Indicate the Need for Function Members That Retrieve (Return) Property Values

Sentences with the verbs below indicate specific properties and the need for member functions (methods) that provide a way to retrieve the property values:

get,         provide,         return,         retrieve

For example, the sentence:

Users should be able to get the name and age of persons

might well indicate that persons have the properties 'age' and 'name' and that there should be member functions to retrieve the values of these properties.

Such member functions have a similar design pattern:

    Purpose:   To return the square footage of an office
    Goal:        The square footage is returned

    Receives: NONE
    Returns:   the square footage (an integer)


Pattern D. The List Data Structure Implemented as an Array

Whenever one is required to add to or delete elements from a collection, the list data structure should be considered. One of the simplest ways to implement a list is with an array declared as one data member of a class.

    Pattern:
      Declare a class as follows:

      class class name

      {public:

        A constructor
        Member functions to add, delete, etc.

      private:

        int the number of elements presently on the list ('numElements')
        int the maximum number of elements on the list ('maxElements')
        declaration of the array to hold the elements
      };

    Note 1: See Chapter 8, Section III of "The Story of C++" for discussion and coded example of this.

    Top of Section Main Menu