Java Reference
In-Depth Information
the operations that pertain to them in an object. This approach solves many of the problems
inherent in procedural programming. The object-oriented programming approach organizes
programs in a way that mirrors the real world, in which all objects are associated with both
attributes and activities. Using objects improves software reusability and makes programs
easier to develop and easier to maintain. Programming in Java involves thinking in terms of
objects; a Java program can be viewed as a collection of cooperating objects.
10.2
Is the BMI class defined in Listing 10.4 immutable?
Check
Point
10.4 Class Relationships
To design classes, you need to explore the relationships among classes. The common
relationships among classes are association , aggregation , composition , and
inheritance .
Key
Point
This section explores association, aggregation, and composition. The inheritance relationship
will be introduced in the next chapter.
10.4.1 Association
Association is a general binary relationship that describes an activity between two classes.
For example, a student taking a course is an association between the Student class and the
Course class, and a faculty member teaching a course is an association between the Faculty
class and the Course class. These associations can be represented in UML graphical notation,
as shown in Figure 10.4.
association
Take
Teach
5..60
0..3
1
Student
Course
Faculty
Teacher
F IGURE 10.4
This UML diagram shows that a student may take any number of courses, a
faculty member may teach at most three courses, a course may have from five to sixty stu-
dents, and a course is taught by only one faculty member.
An association is illustrated by a solid line between two classes with an optional label that
describes the relationship. In Figure 10.4, the labels are Take and Teach . Each relationship
may have an optional small black triangle that indicates the direction of the relationship. In
this figure, the direction indicates that a student takes a course (as opposed to a course taking
a student).
Each class involved in the relationship may have a role name that describes the role it plays
in the relationship. In Figure 10.4, teacher is the role name for Faculty .
Each class involved in an association may specify a multiplicity , which is placed at the
side of the class to specify how many of the class's objects are involved in the relationship
in UML. A multiplicity could be a number or an interval that specifies how many of the
class's objects are involved in the relationship. The character * means an unlimited number
of objects, and the interval m..n indicates that the number of objects is between m and n ,
inclusively. In Figure 10.4, each student may take any number of courses, and each course
must have at least five and at most sixty students. Each course is taught by only one faculty
member, and a faculty member may teach from zero to three courses per semester.
In Java code, you can implement associations by using data fields and methods. For exam-
ple, the relationships in Figure 10.4 may be implemented using the classes in Figure 10.5. The
multiplicity
 
 
 
Search WWH ::




Custom Search