Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ANALYSIS
Classes - Relationships
generalization, aggregation
and
composition
Spring 910 1
MIS326 Generalisation
A relationship between a more general element and a
more specific element
The more specific element is entirely consistent with the
more general element but contains more information
An instance of the more specific element may be used
where an instance of the more general element is
expected
Spring 910 2
MIS326 Example: class generalisation
child
subclass
Square Circle Triangle
descendent
more specific elements
A generalisation hierarchy
Spring 910 3
Spring 910 4
MIS326 Class inheritance
Subclasses inherit all features of their superclasses:
– attributes
– operations
– relationships
Subclasses can add new features
Subclasses can override superclass operations
We can use a subclass instance anywhere a superclass
instance is expected
Spring 910 5
MIS326 Overriding
Shape
draw( g : Graphics )
getArea() : int
getBoundingArea() : int
Square Circle
draw( g : Graphics ) draw( g : Graphics )
width x height getArea() : int getArea() : int π x radius2
Vehicle
What’s wrong
with this model?
JaguarXJS Truck
Spring 910 7
MIS326 Polymorphism
Polymorphism = "many forms"
A Canvas object has a collection of Shape objects
– A polymorphic operation has where each Shape may be a Square or a Circle
many implementations
Canvas
– Square and Circle provide
1
implementations for the
polymorphic operations
Shape::draw() and shapes *
Shape::getArea() Shape
polymorphic draw( g : Graphics ) abstract
All concrete subclasses of
operations getArea() : int superclass
Shape must provide concrete
getBoundingArea() : int
draw() and getArea() operations
because they are abstract in the
superclass
– For draw() and getArea() we Square Circle
can treat all subclasses of
draw( g : Graphics ) draw( g : Graphics )
Shape in a similar way - we
getArea() : int getArea() : int
have defined a contract for
Shape subclasses concrete subclasses
Spring 910 8
MIS326 What happens?
Each class of object
has its own
implementation of the s1:Circle
draw() operation 1.draw()
On receipt of the
draw() message, 2.draw() s2:Square
each object invokes :Canvas 3.draw()
the draw() operation
specified by its class s3:Circle
4.draw()
We can say that each
object "decides" how
s4:Circle
to interpret the draw()
message based on
its class
Spring 910 9
Account
1 * withdraw() : int
Bank calculateInterest()
deposit()
Aggregation Composition
MIS326 Aggregation
Spring 910 12
MIS326 Aggregation
Aggregations are also used when a class seems to have
over-complicated attributes - that is the attribute structure
is complex
It is sometimes clearer for the analysis and design to
create a new class which “is a part of” the other class
– it is nothing to do with inheritance - more like a special
type of association
An aggregate is a container for other objects
An aggregation relationship can also be read as “consists
of” or “is made of”
Spring 910 13
HomeComputer
1 1 1 1 2
2
Mouse Keyboard Case Monitor Speaker
1 connectedTo
* 1 1..* 1 1 1
RAM CPU HardDrive CDRom SoundCard GraphicsCard
1
1
connectedTo
Spring 910 14
MIS326 Composition
Spring 910 15
Window
Spring 910 16