1 Introduction
2 The Design Recipe
3 Methods for Simple Classes
4 Methods for Self-Referential Data
4 Method design for self-referential data
Version: 5.2.1

Designing Methods

1 Introduction

We start with designing data - designing classes of data that are connected to each other in a systematic way, showing that the Design recipe for Data Definitions can be used virually without change in a completely different language than we have used in the first part.

We start designing methods in the functional style, i.e., every method produces a new value (primitive or a new object) and makes no changes in the existing data. This allows us to use a very simple language, one where the only two statements are return and if (condition) then statement else statement.

The programs we provide give you examples of the progressively more complex data (class) definitions, and illustrate the design of methods for these class Hierarchies.

The design of methods follows the same Design Recipe we have seen before. The only difference here is that for classes that represent a union type (for example classes Circle and Rectangle that are both a part of the union type Shape, the conditional statement that would be used in some programming languages is replaced by the dynamic dispatch into the class whose constructor has been used to define the specific object.

2 The Design Recipe

The DESIGN RECIPE teaches us to design each method in six steps:

3 Methods for Simple Classes

Below is an example of the design of a method that computes the number of pixels in a photo image:

4 Methods for Self-Referential Data

Let us now consider the class hierarchy that represents a list of photo images. We first design the method that counts the images in our list. (We use the simpler version of the class Photo.

Note: Of course, you will quickly realize that this method will look the same regardless of what are the pieces of data contained in the list. We will address that issue later on, once we are comfortable with dealing with lists that contain specific items.

The class diagram for a list of photo images is shown below:

             +--------------+

             | IListOfPhoto |<----------------+

             +--------------+                 |

             +--------------+                 |

                    |                         |

                   / \                        |

                   ---                        |

                    |                         |

        ------------------------              |

        |                      |              |

+---------------+    +-------------------+    |

| MTListOfPhoto |    | ConsListOfPhoto   |    |

+---------------+    +-------------------+    |

+---------------+  +-| Photo first       |    |

                   | | IListOfPhoto rest |----+

                   | +-------------------+

                   v

          +----------------+

          | Photo          |

          +----------------+

          | String name    |

          | String kind    |

          | int width      |

          | int height     |

          | int bytes      |

          +----------------+

4 Method design for self-referential data

Below is an example of the design of a method that counts the number of pictures in a list of photo images.

The method deals with IListOfPhotos. We have an interface

IListOfPhotos

and two classes that implement the interface:

MTListOfPhotos and ConsListOfPhotos. When the Design Recipe calls for the method purpose statement and the header, we include the purpose statement and the header in the interface IListOfPhotos and in all the classes that implement the interface.

Including the method header in the interface serves as a contract that requires that all classes that implement the interface define the method with this header. As the result, the method can be invoked by any instance of a class that implement the interface - without the need for us to distinguish what is the defined type of the object.

We can now proceed with the Design Recipe.