2.1 The design of the Friend class


In order to give a practical introduction to these concepts, a class whose major purpose is to illustrate their properties will be designed, developed and described. An associated client program will then illustrate the concepts by using its actions. The class is intended to store and manipulate details of a Friend, its class diagram is shown in Figure 2.1.


Figure 2.1. The Friend class diagram.

This class diagram differs from the diagrams given in the previous chapter not only by having more actions, but also by giving some indication of its internal structure and by indicating, at the top, that it is an extension of the Java Object class. The purpose of a class diagram is firstly to assist a developer with the design of the class and, secondly, to inform a developer wanting to use the class of the actions, and other resources, which it makes publicly available.

Any parts of the design which are shown on the right are considered fully public and can be used by any client of the class. Any parts which are fully contained within the diagram are fully private and are the sole concern of the class. They need not be shown and, in some circumstances, the implementation of the class need not actually use them. They are only present in the final version of the class diagram to assist with its understanding.

On this diagram there are four private attributes and as they have single lined boxes and are expressed with noun phrases, they are assumed to be data attributes. The first two, shown in normal lined boxes, are instance variable attributes named friendsName and friendsAge. The last two, shown in heavy lined boxes, are class wide attributes named numberOfFriends and PERSONAL_GREETING. The use of capitalisation indicates that the last of these attributes is a constant attribute, the others are variable.

The first action on the design is called Friend(), which is also the name of the class. This indicates that it is a constructor for the class. The purpose of a constructor is to place the class instance into a well defined initial state when it is created; this usually involves setting the values of some, or all, of the instance data attributes. There are two instance data attributes in this design and the theirName and theirAge data flows into the constructor indicate that the value of the attributes will be specified by these arguments when the constructor is used.

The next two actions contain verb phrases which end in the term Is which, by the convention used in this book, indicates that they are enquiry actions retrieving the value of a private data attribute on behalf of a client. The friendsNameIs() action will return the name of the friend and the friendsAgeIs() their age, as shown by the data flow originating from the action. The remaining instance action is the birthday() action which requires no arguments and supplies no data. It purpose is to record the birthday of a friend by increasing the friendsAge instance attribute by 1.

The next action, numberOfFriendsIs(), is also an enquiry action returning the value of the class wide data attribute numberOfFriends . Likewise the last action greetingIs() returns the personal greeting to be used when addressing a friend, stored in the class wide constant PERSONAL_GREETING. As these two actions are class wide, as opposed to instance, actions, they are shown in heavy lined boxes.