Sei sulla pagina 1di 8

MIS326

OBJECT ORIENTED MODELING


WITH UML

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

more general element parent


superclass
Shape base class
ancestor
specialisation

“is kind of”


generalisation

child
subclass
Square Circle Triangle
descendent
more specific elements

A generalisation hierarchy

Spring 910 3

MIS326 How to identify Generalization


 From the existing class diagram identify common behavior
(attributes and operations)
 Inheritance is NOT a relationship between objects but a
relationship to support the correct classification of objects
 Suggestions:
– Types of employee, types of stock, types of payment, ...
 Why use it?
– Generalization - exploit class similarities - avoids
duplication
– Specialization - highlight class differences - each sub
class contains only those specific details which make it
special

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

 Subclasses often need to override superclass behaviour


 To override a superclass operation, a subclass must
provide an operation with the same signature
– The operation signature is the operation name, return type and
types of all the parameters
– The names of the parameters don’t count as part of the signature
Spring 910 6
MIS326 Exercise

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

MIS326 Another example

Account
1 * withdraw() : int
Bank calculateInterest()
deposit()

ShareAccount CheckingAccount DepositAccount


withdraw() withdraw() withdraw()
calculateInterest() calculateInterest() calculateInterest()
deposit()

Notice that we have overridden the deposit() operation even though it is


not abstract. This is perfectly legal, and quite common, although it is
generally considered to be bad style
Spring 910 10
MIS326 Aggregation and composition
UML defines two types of association:

Aggregation Composition

Some objects are weakly Some objects are strongly


related like a computer and related like a tree and
its peripherals its leaves
Spring 910 11

MIS326 Aggregation

A Computer may be attached to 0 or more


aggregation is a whole–part relationship Printers
At any one point in time a Printer is
0..1 0..*
connected to 0 or 1 Computer
Computer Printer
Over time, many Computers may use a given
whole or aggregation part Printer
aggregate
The Printer exists even if there are no
Computers
The Printer is independent of the Computer

 The aggregate can sometimes exist independently of the parts


 The parts can exist independently of the aggregate
 The aggregate is in some way incomplete if some of the parts are missing
 It is possible to have shared ownership of the parts by several aggregates
 Aggragation is transitive

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

MIS326 Aggregation Example (hierarchy)

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

composition is a strong form of aggregation The buttons have no independent


always 0..1 or 1 existence. If we destroy the mouse,
we destroy the buttons. They are an
1 1..4 integral part of the mouse
Mouse Button
composition Each button can belong to exactly 1
composite part mouse

 The parts belong to exactly 0 or 1 whole at a time


 The composite has sole responsibility for the disposition of all its parts.
This means responsibility for their creation and destruction
 If the composite is destroyed, it must either destroy all its parts, OR give
responsibility for them over to some other object
 Composition is transitive

Spring 910 15

MIS326 Composition Example

Window

scrollbar 2 title 0..1 pane 1

Slider TitleBar Pane

Spring 910 16

Potrebbero piacerti anche