Modularity ========== Object-oriented programming can lead to modular code. When you are experienced with an object-oriented programming style, you might be able to define classes and methods with the right modularity from the start. Novices, however — and even experienced object-oriented programmers who are attacking large problems — may find that they discover opportunities for sharing as they begin to implement classes and methods. The dynamic aspects of Dylan support an evolutionary approach to programming, so it is easy to continue to refine your implementation and to design as you go. In this chapter, we show an evolutionary approach to programming, as we define classes that represent different kinds of positions. We start out with one approach, and gradually refine it to achieve greater modularity. We illustrate one new Dylan feature: abstract classes. Starting in this chapter, and continuing throughout the rest of the book, we take the approach of editing and compiling source code. Now and then, we use a listener to call a function and show the function’s output. Whenever we use a listener, we show the ``?`` prompt. Requirements of the position classes ------------------------------------ To predict when an aircraft will arrive at the airport, we need to know the speed of the aircraft relative to the ground, and the distance the aircraft is from the airport. Thus, we need to represent the positions of objects, such as airports and aircraft, to compute distances. We shall use two ways to express the position of an object. First, we use latitude and longitude to indicate the *absolute position* of the object. Second, we describe the position of the object relative to a second object. For example, a particular aircraft might be 200 miles west of a given airport. This kind of description is a *relative position*. We shall define the classes ```` and ````. The slots of ```` will store information about the latitude or longitude of that position. The slots of ```` will include a distance (such as 200 miles), and a direction (such as south). We need to provide ``say`` methods for absolute and relative positions. The following sample calls show the output that we want to achieve: .. code-block:: dylan-console ? say(*my-absolute-position*); => 42 degrees 19 minutes 34 seconds North latitude => 70 degrees 56 minutes 26 seconds West longitude ? say(*her-relative-position*); => 30 miles away at heading 90 degrees Initial class definitions ------------------------- We start with these simple, initial class definitions: .. code-block:: dylan // Superclass of all position classes define class () end class ; define class () slot latitude; slot longitude; end class ; define class () slot distance; slot angle; end class ; These initial definitions show the inheritance relationships among the classes, and the names of the slots show the information that the classes must provide. At this point, we omit the type declarations of the slots, which is equivalent to specifying the type ````. We will fill in the implementation later, by deciding on the types of the slots, and providing the ``say`` methods. Our requirements mention only ```` and ````, but we choose to define a superclass of both of them, named ````. .. topic:: Modularity note: The benefits of defining the ```` class are these: - The ```` class creates an explicit relationship between the other position classes, which are related conceptually. - We can use the ```` class as the type of a slot or other object, in cases where either an absolute or relative position is appropriate. Abstract classes ---------------- We intend that the ```` class will not have direct instances. Any position objects should be direct instances of ```` and ````. In Dylan, a class that is intended to be a superclass and not to have direct instances is an *abstract* class. A class that is intended to have direct instances is a *concrete* class. By default, a user-defined class is concrete. To define an abstract class, you declare it to be abstract in the ``define class`` form. For example: .. code-block:: dylan // Superclass of all position classes define abstract class () end class ; The ``