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. 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 Method Members That Change
Property Values
Sentences with the verbs below indicate specific properties and the need for
member methods (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 method must exist to enable this property to be
changed.
Such 'Change' member methods all have a similar design pattern:
Pattern C. Verbs that Indicate the Need for Method Members That Retrieve
(Return) Property Values
Sentences with the verbs below indicate specific properties and the need for
member 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 methods to retrieve the values of these
properties.
Such member methods have a similar design pattern:
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:
Define a class as follows:
class class name
{ 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
}
{ public:
Define a public constructor and methods to add, delete, etc.
}
|