Example - Lines and Pens

Here is an example, which you may wish to examine on both this slide and the next as you hear this description.

Suppose we have an object that knows just a single message, drawFromTo, which is used to draw a line from one place to another.

We want to build a new object that not only draws, but remembers its location. Such a value is sometimes called a pen. We simply create a new object, give it location setting values, and use the delegate to draw the lines.

If we want to create a second pen, we say it is just like the first, and only redefine the methods that get and set the x and y coordinates.

suppose now we want to create a LOGO-style turtle, an object that has a location and a direction and can draw. Well, a turtle is like a pen, only it has a direction and a move command. So we delegate from a pen, and write the new methods.

So we can build a whole system using nothing by the idea ``i'm just like this other object over there, and anything I don't understand will be passed on to the other object''.

[audio] [real] Text to accompany slide12, in Chapter 20 of An Introduction to Object-Oriented Programming