The class diagram is central to the development of an object- oriented system because it forms the basis for the structure of the software. Objects and classes are the basic building blocks of any object-oriented system. During analysis, the class diagram models
THE CLASS DIAGRAM COMMON PROBLEMS
137
classes in the problem domain. Features of the real-life objects that are of relevance to the system are modelled as attributes, and real- life links are modelled as relationships. During design activities we begin to think in terms of a software solution rather than just modelling the real-world situation. We add classes required by the logic of the solution that are not part of the real world, for example interface and control classes. The implementation model will add classes required to implement it, for example classes from the programming language~ library such as mouse listeners, buttons and windows. However, the classes identified during analysis will still be in the code that implements the system.
The class diagram is the basis of the overall analysis model.
Interaction diagrams (see Chapter 6) cannot be attempted until the class diagram has been constructed. Interaction diagrams identify the classes involved in producing the functionality of each use case. They specify, scenario by scenario, the messaging between objects that is required in the execution of the use cases. State diagrams (see Chapter 7) are also based on the class diagram. The state diagram models, class by class, all the things that can happen to an object and all its possible reactions. It focuses on one class of objects at a time and shows how all the scenarios affect it.
Common problems
1 Do the stages of building a class diagram have to be carried out in the same order as in the list?
As we said at the beginning of the chapter, there are many ways of building a class diagram. The list of stages is only a guideline, but it helps beginners to the subject to get started. Once you have constructed a number of class diagrams, your own experience will be the best guide as to how to proceed.
The Wheels class diagram does not contain any a g g r e g a t i o n - why is that?
It is not compulsory to use aggregation, or any of the relationship constructs, in a class diagram. Aggregation is really only useful when there is an obvious 'consists-of' or 'is-made- up-of' relationship and it is important to document this. For example, in a restaurant system it might be useful to document that a set meal consists of three food courses and a drink. In the Wheels system there are no obvious aggregation relationships that it is important to record, so we have used association only.
3 How do I k n o w what level of detail to go down to in a data dictionary?
138 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
This depends on how the data dictionary will be used. If it is to be used simply to document the system developer~
understanding of the current system and to support discussions with the client, labels such as 'name' or 'date' will be self- explanatory; no more detail is required. During design, however, the developer is thinking ahead to the detail required in an automated system. Decisions have to be made that were unnecessary earlier, for example, about input and output formats for dates and how they are going to be represented internally.
Chapter summary
The class diagram defines the software architecture and the internal structure of the objects in an object-oriented system; the classes we model in the class diagram form the basis of the classes in the code.
In this chapter we limit the discussion to the analysis class diagram;
this only models classes which have meaning in the application domain. Classes relating to the design and implementation of the solution will be discussed in Chapters 9 and ~o. The association relationships we identify in the analysis diagram model real-life relationships between objects and evolve through various iterations of the model to define the navigable paths between the objects. The inheritance relationships we identify clarify and simplify the model and allow us to build in some future-proofing of our system.
The stages in the construction of a class diagram are identifying objects and deriving classes, identifying attributes, identifying relationships, writing a data dictionary, identifying operations and writing operation specifications. In this chapter we cover the first four of these stages, relating to classes, attributes, relationships and the data dictionary. We also show how managing the model in larger systems can be facilitated by using a package to split classes into logical groups.
Bibliography
Bennett, S., McRobb, S. and Farmer, R. (2002)
Object-Oriented Systems Analysis and Design Using UML
(2nd edition), McGraw-Hill, London.Britton, C. and Doake, J. (2000)
Object-Oriented Development: A Gentle Introduction,
McGraw-Hill, London.Systems
Fowler, M. (2000)
UML Distilled: A Brief Guide to the Standard Object
Modeling Language
(2nd edition), Addison-Wesley, Reading, MA.THE CLASS DIAGRAM
EXERCISES 139
Larman, C. (1998)
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design,
Prentice Hall, New Jersey.Quatrani, T. (1998)
Visual Modeling with Rational Rose and UML,
Addison-Wesley, Reading, MA.Rumbaugh, J., Blaha, M., Premerlani, W., Eddy, F. and Lorensen, W. (1991)
Object-Oriented Modeling and Design,
Prentice-Hall, Englewood Cliffs, NJ.Stevens, P., with Pooley, R. (2ooo)
Using UML. Software Engineering with Objects and Components
(updated edition), Addison-Wesley, Harlow.Quick check questions
You can find the answers to these in the chapter.
The class diagram starts with classes that are part of the applica- tion domain. What sorts of classes are added during later phases of development?
b W h a t is use case realization?
c W h a t are the main stages in building a class diagram?
d List four categories of objects.
List eight reasons for rejecting candidate objects when looking for classes.
Give one reason for modelling a concept such as Hire as a class rather than as a relationship.
g W h a t is the purpose of a data dictionary?
h What are packages? What are they used for?
Exercises
5.1 Suggest objects you would expect to find in the following systems:
a A system for a medical centre.
b A system for a video hire shop.
c A system for a car park which allows entry with an ID card.
d A system allocating equipment to local schools.
140 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
5.2
5.3
5.4
5.5
5.6
Identify the categories (people, organizations, physical things, conceptual things) of the objects you suggested in your answer to Question 5.1.
From the case study descriptions below, prepare a list of nouns for consideration as candidate objects. Reject those that would not form suitable objects, giving reasons for your rejection. List the remaining objects.
Mr Major, the town~ only dentist, has a computer system to help him keep track of patients' appointments and dental treatment. Mr Major~ receptionist makes appoint- ments with patients either when they phone up or when they are back in the waiting room after treatment. Some- times patients phone to cancel appointments or to change them. Mr Major keeps notes on the system about his patients' treatments- these are updated each time he sees a patient. The receptionist also makes out bills for patients and records payments on the system.
b Your local railway station is going to install automatic ticket dispensing machines. Each machine will be able to give passengers up-to-date train timetable and ticket price information. The machines must also issue tickets and transfer statistics about ticket sales to a central com- puter system. Railway staff must be able to update ticket prices and timetable information.
Suggest attributes for the following classes:
a A Customer in a banking system.
b A library Member.
A university Lecturer in a system for a university human resources department.
d A Student in a university enrolment system.
e A Patient in a dentist's system.
Items a-f in Table 5.2 list object-oriented concepts. Items 1- 6 list short definitions. Match the concept to the definition that best describes it.
What information appears on an object diagram (such as the one in Figure 4.7 in Chapter 4) that is not shown on a class diagram such as the one in Figure 5-15 (in this chapter)?
THE CLASS DIAGRAM EXERCISES
141
Table 5.2" Concepts and definitions for Exercise 5.5
Concept Definition
a) application domain x. mechanism for grouping modelling items, e.g. classes b) multiplicity 2. area of study, e.g. business application
c) domain model 3. group of classes relating to a single use case d) collaboration 4. a class diagram that sets out to model all of the
classes in the problem domain together (not in separate use cases)
e) noun analysis 5. describes the number of instances allowed to participate in an association or aggregation relationship
f) package 6. identifying objects and classes from nouns in a written description of the problem
Figure 5.20
5.7 This exercise will help you to revise aggregation and inheritance.
An art gallery organizes exhibitions that consist of exhib- its. An exhibit may be an oil painting, a watercolour, a piece of sculpture or a photograph. Draw a class diagram to repre- sent this information.
5.8 Study the class diagram in Figure 5.20 and answer the questions that follow it.
a Does a customer have to place an order?
b What does an order consist of?.
c Can a payment be for more than one order?
Payment
Customer
1 1..:~
1 Order
OrderLine
Product
Book
I
CDI
Video
Class diagram for Exercise 5.8
142 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
5.9
5.xo
5.11
5.12
d How many products are there in an order line?
e What is the relationship between a CD and a product?
f Can a product appear on more than one order line?
This exercise will help you to construct a class diagram.
Build up the diagram by drawing the separate parts according to the information given below.
a A football club has two grounds.
b Each ground consists of two or three pitches and a club- house.
c The football club has lots of members.
d A member may be a playing member or a social member, and a playing member may be an adult member or a jun- ior member.
e A playing member may be chosen to play for one or more teams, and each team has 11 playing members.
f Each team plays a number of fixtures.
A large film organization has several cinemas, which each consists of up to ten separate screens. Filmgoers can book seats in person, by phone or on the Internet. The organization keeps a record of the names and addresses of regular filmgoers for marketing purposes. Draw a class diagram to represent this information.
The Keep Well Medical Centre is run by five doctors with three practice nurses. When a patient calls for an appointment, he or she usually sees the same doctor, but at busy times patients may see any of the doctors or nurses. Once a patient has been seen by the doctor or nurse, the medical records are updated and the doctor may also write out a prescription for the patient. Draw a class diagram to represent this information.
b Sometimes the doctor considers that the patient needs further tests. These tests may be routine or intensive;
they are carried out at one of the local hospitals. Extend the diagram that you drew for part (a) to include this extra information.
Study the class diagram in Figure 5.21 and answer the questions that follow it.
THE CLASS DIAGRAM EXERCISES 143
Branch
CashMachine
* Customer
* Transaction
1 ..,J Account
I §
CurrentAccount
I I I I
Deposit Withdrawal Balance Statement
SavingsAccount
I
Figure 5.21 Class diagram for Exercise
5.12a
a. Remember that where the multiplicity is one only, the "I" symbol may be omitted (see Chapter 4).
5 . 1 3
5.14
a Does a customer have to have at least one account?
b Can a customer have both current and savings accounts?
c Is a deposit part of a transaction or a kind of transaction?
d How is a cash machine related to a branch?
Oscar~ Films is a shop that rents out films on video and DVD.
Oscar has a large number of customers and keeps a record of the name and address of everyone who hires a video or DVD.
Regular customers can become members of the shop; this entitles them to certain privileges, such as being able to take out more films at one time. In the case of members, Oscar also keeps a record of their membership number, phone number and the types of film they like to watch. He also keeps records of all films and the copies he stocks of each.
a Draw a class diagram to represent this information.
b Write a data dictionary description for Member.
Study the data dictionary definition below and answer the questions that follow it.
Order = orderHeader + {orderLine} + total
orderHeader - customerDetails + (deliveryAddress) +dateRequired
customerDetails = (title)+ 1 {[firstName J initial]} 2 + surname + customerAddress + phone + (email)
144 A STUDENT GUIDE TO OBJECT-ORIENTED DEVELOPMENT
5.15
5.16
orderLine = product# + productDescription + quantity + productCost + costOfLine
total = costOfOrder + (deliveryCharge) + amountOwing a Does a delivery address have to be recorded?
b Does the date required have to be recorded?
c How many first names or initials are recorded for each customer?
d Can an order have more than one order line?
The delivery charge only applies if a customer lives more than ten miles away. How would you document this in the definition?
It has been decided to modify the order line part of the definition to include product category. This can take one of five values: menk womenk children,, outdoor, sport.
Add this information to the data dictionary definition.
A textbook consists of a preface, between six and ten chapters, an optional glossary and a bibliography. Each chapter has an introduction, several sections and a summary, and the glossary contains lots of terms with their definitions. Write a data dictionary definition for a textbook as described here.
Figure 5.22 shows an example card used to record details of offers for properties in an estate agent~ business. Write a data dictionary definition of the information held on the cards.
THE CLASS DIAGRAM EXERCISES
145
Figure 5.22 Estate agent's card for recording offers on properties
This Page Intentionally Left Blank