Thinking the OO way
Object-oriented design is a different way of designing programs, where the
behavior is associated with objects. To illustrate, you once again make tea
and soup. This time, you don’t immediately leap into action. Instead, you act
like a detective entering a crime scene. You step back, survey the domain
(the kitchen), and try to understand what’s in it. In other words, you think
objects first.
? You see a kettle. You consider the kettle for a while and then say, “I can
add water to it; I can pour water from it.”
? You look at a teapot. As it looks back, you think, “I can add water to
this, too, and pour water from it, as well, just like I can with a kettle.”
Both the kettle and teapot appear to be able to take in and pour out water.
They both also have state (the water being held). Therefore, you generalize
that each of these objects is a vessel. You also think about the characteristics
of these objects, that is, their attributes — such as their volume, color, and
material.
You think about the other items in the kitchen, namely, the stove, the faucet,
the cups and bowls, the various sorts of spoons, and the food items: soup,
tea, and bouillon, even the kitchen clock. You think about their capabilities,
as well. You try to generalize the kinds of objects into higher-level concepts,
such as vessel.
Rather than thinking of a recipe only as a sequence of actions, you realize that
a recipe is also an object. After all, you can create recipes, tear up recipes,
and share recipes. A recipe is indeed something tangible; therefore, it’s an
object, an object with capabilities. When you follow a recipe, you can imagine
that you’re running it. So, you can make run a capability of a recipe. You can
also make setting up and cleaning up responsibilities of a recipe object. Does a
recipe have attributes? How about the level of difficulty of a recipe — easy or
hard? What about its author (a string)? Or how many people it serves (an integer)?
Finally, recipes clearly have a generalization hierarchy, with Making Tea
and Making Soup being specializations of a generic Recipe.
No comments:
Post a Comment