Session1 OOAD
Session1 OOAD
Programming?
Classes
A class is a description of a set of
ClassName objects that share the same attributes,
operations, relationships, and semantics.
attributes
Graphically, a class is rendered as a
rectangle, usually including its name,
operations attributes, and operations in separate,
designated compartments.
1
Class Names
The name of the class is the only required
ClassName tag in the graphical representation of a
class. It always appears in the top-most
attributes compartment.
operations
2
Class Attributes
Person
3
Class Attributes (Cont’d)
Attributes are usually listed in the form:
Person attributeName : Type
/ age : Date
4
Class Attributes (Cont’d)
Person
5
Class Operations
Person
name : String
address : Address
birthdate : Date
ssn : Id
eat Operations describe the class behavior
sleep and appear in the third compartment.
work
play
6
Class Operations (Cont’d)
PhoneBook
7
Depicting Classes
When drawing a class, you needn’t show attributes and operation
in every diagram.
8
Class Responsibilities
A class may also include its responsibilities in a class diagram.
SmokeAlarm
Responsibilities
10
What is an object?
11
So, what are objects?
= +
15
Basic Terminology:
Behaviour and Messages
16
Interfaces
An interface is a named set of
operations that specifies the behavior
<<interface>> of objects without showing their inner
ControlPanel structure. It can be rendered in the
model by a one- or two-compartment
rectangle, with the stereotype
<<interface>> above the interface
name.
17
Interface Services
18
Interface Realization Relationship
A realization relationship
<<interface>>
connects a class with an
ControlPanel
interface that supplies its
specifier behavioral specification. It is
rendered by a dashed line with
a hollow triangle towards the
specifier.
implementation
VendingMachine
19
Interfaces
inputStream
FileWriter
{file must not be locked}
20
Encapsulation
21
Inheritance
organization of classes into a hierarchical
inheritance tree
data and behavior associated with classes
higher in the tree are accessible to those
classes lower in the tree
terminology
ancestor/descendant
superclass/subclass
generalization/specialization
22
Single Inheritance
23
Multiple Inheritance
24
Inheritance Diagram
25
Another Inheritance
Diagram
26
Noun analysis
Consider a simplified vending machine, which is used to dispense soft
drinks. The vending machine consists of a coin slot for inserting coins,
a return tray for returning the customer's money and three buttons
used to select Pepsi (tm), Coke (tm) or Dr. Pepper (tm). If the customer
inserts coins and does not press a button within one minute, the coins
will be returned automatically (no coin-return lever). If the customer
selects a beverage which is out-of-stock (none left), the coins will also
be returned. This vending machine does not provide change - it is up to
the customer to insert only enough money to purchase a drink. If
sufficient coins are inserted and a button with available beverages is
pressed, the appropriate drink is dispensed, the corresponding button is
illuminated for five seconds, and the coin slot moves the coins to the
general storage area (i.e. the purchase has finished).
27
Domain Model - Initial Class
Diagram
Inserts
Contains
coins CoinSlot 1 1 0..*
into Contains
amount 3 Shelf Drink
1 1
1
Vending Contains
1
Customer Machine 1
1
Button
Pushes Contains
id
3 Contains 1
litUp
ReturnTray
28
CRC Card Layout.
29
30
So how do you create CRC models?
Iteratively perform the following
steps:
31
Define collaborators. A class often does not have sufficient information
to fulfill its responsibilities. Therefore, it must collaborate (work) with other
classes to get the job done. Collaboration will be in one of two forms: a
request for information or a request to perform a task. To identify the
collaborators of a class for each responsibility ask yourself "does the class
have the ability to fulfill this responsibility?". If not then look for a class
that either has the ability to fulfill the missing functionality or the class
which should fulfill it. In doing so you'll often discover the need for new
responsibilities in other classes and maybe even the need for a new class
or two.
Move the cards around. To improve everyone’s understanding of the
system, the cards should be placed on the table in an intelligent manner.
Two cards that collaborate with one another should be placed close
together on the table, whereas two cards that don’t collaborate should be
placed far apart. Furthermore, the more two cards collaborate, the closer
they should be on the desk. By having cards that collaborate with one
another close together, it’s easier to understand the relationships between
classes.
32
33
Relationships
In UML, object interconnections (logical or physical), are
modeled as relationships.
• dependencies
• generalizations
• associations
34
Dependency Relationships
A dependency indicates a semantic relationship between two or
more elements. The dependency from CourseSchedule to
Course exists because Course is used in both the add and
remove operations of CourseSchedule.
CourseSchedule
Course
add(c : Course)
remove(c : Course)
35
Generalization
Relationships
Person
A generalization connects a subclass
to its superclass. It denotes an
inheritance of attributes and behavior
from the superclass to the subclass and
indicates a specialization in the subclass
of the more general superclass.
Student
36
Generalization Relationships
UML permits a class to inherit from multiple superclasses,
although some programming languages (e.g., Java) do not permit
multiple inheritance.
Student Employee
TeachingAssistant
37
Association Relationships
If two classes in a model need to communicate with each other,
there must be link between them.
Student Instructor
38
Association Relationships
We can indicate the multiplicity of an association by adding
multiplicity adornments to the line denoting the association.
Student Instructor
1..*
39
Association Relationships
Student Instructor
1..*
40
Association Relationships
We can also indicate the behavior of an object in an association
(i.e., the role of an object) using rolenames.
41
Association Relationships
We can also name the association.
membership
Student Team
1..* 1..*
42
Association Relationships
We can specify dual associations.
member of
1..* 1..*
Student Team
1 president of 1..*
43
Association Relationships
We can constrain the association relationship by defining the
navigability of the association. Here, a Router object requests
services from a DNS object by sending messages to (invoking
the operations of) the server. The direction of the association
indicates that the server has no knowledge of the Router.
Router DomainNameServer
44
Association Relationships
Associations can also be objects themselves, called link classes
or an association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
45
Association Relationships
next
LinkedListNode
previous
46
Association Relationships
We can model objects that contain other objects by way of
special associations called aggregations and compositions.
Engine
Car
Transmission
47
Association Relationships
(Cont’d)
A composition indicates a strong ownership and coincident
lifetime of parts by the whole (i.e., they live and die as a
whole). Compositions are denoted by a filled-diamond
adornment on the association.
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 .. *
48