INTRODUCTION TO OBJECT-ORIENTED DESIGN PART 1 -
Up until now we've largely avoided discussing object-oriented design (OOD). This is
a topic with a variety of methods put forward, and people tend to have strong views
about it. But there are some useful general principles that can be stated, and we will
present some of them in a series of articles.
The first point is perhaps the hardest one for newcomers to OOD to grasp. People will
ask "How can I decide what classes my program should have in it?" The fundamental
rule is that a class should represent some abstraction. For example, a Date class might
represent calendar dates, an Integer class might deal with integers, and a Matrix class
would represent mathematical matrices. So you need to ask "What kinds of entities
does my application manipulate?"
Some examples of potential classes in different application areas would include:
GUI/Graphics - Line, Circle, Window, TextArea, Button, Point
Statistics - Mean, ChiSquare, Correlation
Geography - River, Country, Sea, Continent
Another way of saying it would be this. Instead of viewing an application as
something that performs steps A, B, and C, that is, looking at the program in terms of
its functions, instead ask what types of objects and data the application manipulates.
Instead of taking a function-oriented approach, take an object-oriented one.
One obvious question with identifying potential classes is what level of granularity to
apply. For example, in C++ an "int" is a primitive type, that represents an abstraction
of mathematical integers. Should int be a class in the usual C++ sense? Probably not,
because a class implies certain kinds of overhead in speed and space and in user
comprehension. It's interesting to note that Java(tm), a newer object-oriented
language, also has int, but additionally supports a "wrapper" class called Integer that
represents an integer value. In this way, an application can manipulate integers either
as primitives or as classes.
Consider a slightly more ambiguous case. Suppose that you're writing a Date class,
and you want to express the concept "day of week". Should this be a class of its own?
Besides devising a class for this purpose, at least five other representations are
int dow : 3;