CLASS MODELLING
OVERVIEW
· INTRODUCE OBJECT CLASSES
· HOW TO CREATE AND REFERENCE OBJECTS
· OBJECT CLASS MODELLING
F relationships
F cardinality
· CLASS STRUCTURING
F inheritance
F aggregation
F uses
CLASS MODELLING
· In the early stages of development this model captures the concepts that exist in the problem domain and the associations between them.
· Later stages of development add more detail so that a working system can be implemented.
· At all stages the aim is to capture the concepts and represent them as object classes.
· Icons for class/objects standardised by OMG on
Unified Modelling Language(UML)
OBJECTS AND CLASSES
· Objects in a class have the same structure and are built or instantiated from the same template.
· An object instantiated from a class template is known as an instance of the class.
· In OO Modelling all objects are built from a class definition.
|
Even if there is only one object in the class.
e.g. class
<<instance>> <<instance>> <<instance>>
Rachel:Person |
Rob:Person
objects Joe:Person
age=30 | age=55 | age=55 | |||
· Note that the class defines what the structure of each object should be. Each object has a specific value of the state information e.g. attribute ‘age’.
· All objects of a class have the same structure, it is
the STATE of each object that differs.
OBJECT CREATION AND REFERENCE.
· Each object will be created from a class and have a unique identity by which it can be referenced.
|
Similar to types
· Consider Integer definitions
|
already defined
e.g. Int1: Integer; Int2: Integer
Int1:=2; Int2:=3
This represents a creation of two integers and their references. The references are then used to set the value of the integers.
· Similarly, for objects
Person p1 = new Person(30);
Person p2 = new Person(32);
|
creates objects with ages 30,32 years and references
p1, p2.
p1.changeage:= 28;
p2.changeage:= 32;
|
Use references to send message to change
state i.e. change ‘age’ attribute.
Associations
· Classes do not exist in isolation, as in the problem domain, associations exist between classes of objects.
· Class diagrams show the associations between classes of objects.
Country
has capital
This association represents many possible instances of object combinations. Instances are shown on a object diagram.
ottawa:City | |||
canada:Country | |||
has capital
paris:City | |||
france:Country | |||
Cardinality
· Various constraints may be placed on the associations.
.e.g.
UML CONSTRAINT
3 Exact number
1..2 Range
* Unspecified
e.g1
1 .. * a range between 1 and an unspecified number
0 ..1 either 0 or 1
* an unspecified number
e.g2.
A department manages zero or more projects
A project is managed by one department
742 employees belong to a department
An employee may work on zero or one project.
A project may be worked on by zero or more employees.
· See handout 1(a).
OBJECT MODELLING Exercise 1
1. Draw the class diagram and some example object
diagrams for the following.
A line intersects zero or more points.
A point is intersected by 2 or more lines.
(Hint - draw a few line diagrams)
2. Model the following with an class diagram.
Student studies between 4 and 6 courses, and at least
one student has to study each course.
Lecturer teaches zero or more courses, and every
course must be taught by one lecturer
Each lecturer can head at most one department, and
each department must have a head.
Roles
· A class participating in an association may be thought to be playing a role
|
useful when association direction is unclear
See handout 1(b).
Association Attributes/Objects
· Associations may have attributes and behaviour
· Model with an association class
· Each instance of the association class connects particular instances of the association
|
E.g a particular student with a particular paper
· See handout1(c)
· The association class models any data or behaviour associated with this link.
|
E.g methods to get information about the link
· The association class may then be related to other entities in its own right.
· See handout1(d).
OBJECT MODELLING Exercise 2
· Model the following with class diagrams.
1. A directory may contain zero or more directories.
A directory may, or may not be contained in another
directory.
2. Use a class to model the association ‘position’ in the
following.
Within a company, each employee is assigned a
position in the managerial hierarchy.
The position of any employee has a job title and
salary associated with it.
Hierarchical Associations
· These associations define the ranking or ordering of abstractions within the problem domain.
· Three types -
` is a ` `part of ` ‘uses’
inheritance aggregation client server
Aggregation/Composition
· Aggregation represent whole/part associations.
|
show with an open diamond
· Parts may be shared
· see handout1 (e).
· Composition is a stronger form of aggregation.
|
show with solid black diamond
· The parts are not shared
· E.g a chess-board and its squares
OBJECT MODELLING Exercise 3
· Model the following hierarchies.
1. A stool is composed of 3 legs and a seat. The seat has
a frame and cushion.
2. A computer is composed of one or more monitors, a
system box, an optional mouse, and a keyboard. A
system box, in turn, has a chassis, a CPU, many RAM
chips, and an optional fan.
Inheritance
· Represents `is a` type associations between object classes.
· This view is orthogonal to the whole/part hierarchy i.e. cut across the system in another distinct way.
|
e.g. The aircraft is made up of wings,
body, engine etc. - whole/part.
The aircraft ‘is a’ jet aeroplane.
· see handout1 (f).
· Polygon is called the superclass
· Triangle and square are subclasses
|
A triangle `is a ` polygon
· A subclass inherits all the features of the superclass.
|
Class triangle does not need to specify the attribute
‘number of sides’ since it is inherited from polygon.
Polymorphism and Dynamic Binding
· Within an inheritance hierarchy each Class may have an object instantiation with a reference pointer.
e.g. Polygon p = new Polygon()
Square p = new Square()
Triangle p = new Triangle()
· Polymorphic behaviour allows a reference to point to other types of object in the hierarchy under the ‘is a’
constraint.
· Dynamic Binding allows the pointing of references to be done dynamically during program execution.
e.g p. area - refers to polygon object area
p = s - point p to same object as s
p.area - now refers to square object area
Subclass Overriding
· In addition to inheriting features the subclass may redefine the implementation of some of the superclass operations.
|
Class triangle may redefine ‘area’ using a simpler
formulation than that required for a general
polygon.
May also add further, more specific attributes .e.g.
side 1,2,3 or operations to complement the more
specific nature of the subclass.
· Additions should not override features of the superclass so that the subclass is inconsistent with the original interface semantics.
e.g. ‘area’ in class triangle should still calculate
the area of the shape.
· subclass object may be always be freely substituted
for a superclass object.
e.g.
Polygon p = new Polygon()
Square p = new Square()
Triangle p = new Triangle()
if condition = true then
p:= t
else
p:=s
Sets polygon reference to point
to same object as t or s depending on
condition.
p.area
|
refers to area since all subclasses keep the
area semantics.
· This simplifies reasoning:
what can be deduced about the superclass holds for
the subclasses.
Multiple Inheritance
· Hierarchies may also mix to give multiple inheritance.
· see handout1(g).
· In general there is a tension between inheritance and encapsulation.
|
Since inheritance opens up the superclass structure, it may be thought of as an unravelling of encapsulation under the control of an
‘ is a’ association.
Recursive Definitions
· Many hierarchies show a recursive structure.
· e.g.1
A part consists of zero or more parts
See handout1(h)
· e.g2
program = one or more blocks
blocks = one or more blocks | simple statement
See handout1(i)
OBJECT MODELLING Exercise 4
Model the following hierarchies.
1. Cards, deck, hand, discard pile, draw pile
2. Fluorescent lamp = base, cover, switch, wiring
ballast, twist, starter,mount
Incandescent lamp = base, cover, switch, wiring
socket
3. Car, boat , amphibious vehicle
4. A picture may contain lines, rectangles, text or other
pictures.
OBJECT MODELLING
SUMMARY
· ALL OBJECTS ARE INSTANTIATED/BUILT FROM A CLASS DEFINITION
· EACH OBJECT IS CREATED WITH A REFERENCE POINTER
· OBJECT CLASSES OF PROBLEM DOMAIN MAY BE MODELLED
· OBJECTS OF SOFTWARE ARCHITECTURE MAY BE DERIVED FROM CLASS MODEL
· CLASS MODELS HAVE STRUCTURE