Java Reference
In-Depth Information
combines the power of the procedural paradigm with an added dimension that integrates data
with operations into objects.
In procedural programming, data and operations on the data are separate, and this
methodology requires sending data to methods. Object-oriented programming places data
and 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.9
Is the BMI class defined in Listing 10.4 immutable?
Check
Point
10.7 Object Composition
An object can contain another object. The relationship between the two is called
composition .
Key
Point
In Listing 10.2, you defined the Loan class to contain a Date data field. The relationship
between Loan and Date is composition. In Listing 10.4, you defined the BMI class to contain
a String data field. The relationship between BMI and String is composition.
Composition is actually a special case of the aggregation relationship. Aggregation models
has-a relationships and represents an ownership relationship between two objects. The owner
object is called an aggregating object and its class an aggregating class . The subject object is
called an aggregated object and its class an aggregated class .
An object may be owned by several other aggregating objects. If an object is exclu-
sively owned by an aggregating object, the relationship between them is referred to as
composition . For example, “a student has a name” is a composition relationship between
the Student class and the Name class, whereas “a student has an address” is an aggrega-
tion relationship between the Student class and the Address class, because an address
may be shared by several students. In UML notation, a filled diamond is attached to an
aggregating class (e.g., Student ) to denote the composition relationship with an aggregated
class (e.g., Name ), and an empty diamond is attached to an aggregating class (e.g., Student )
to denote the aggregation relationship with an aggregated class (e.g., Address ), as shown
in Figure 10.6.
aggregation
has-a relationship
composition
Composition
Aggregation
1
1
1
1..3
Name
Address
Student
F IGURE 10.6
A student has a name and an address.
Each class involved in a relationship may specify a multiplicity . A multiplicity could be a
number or an interval that specifies how many of the class's objects are involved in the rela-
tionship. The character * means an unlimited number of objects, and the interval m..n means
that the number of objects should be between m and n , inclusive. In Figure 10.6, each student
has only one address, and each address may be shared by up to 3 students. Each student has
one name, and a name is unique for each student.
multiplicity
 
 
Search WWH ::




Custom Search