![]() |
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:
For example, the sentence: 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: For example, the sentence: 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:
Goal: The square footage data member has a new value Receives: The new square footage (an integer)
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: For example, the sentence: 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:
Goal: The square footage is returned Receives: NONE 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.
class class name {public:
Member functions to add, delete, etc. private:
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.
|