UML and Patterns
Dr. Zhen Jiang
West Chester University
E
-
mail: zjiang@wcupa.edu
url: www.cs.wcupa.edu/~zjiang
Outline
Introduction to UML
Objects and Classes
Class Diagrams
–
Class Icon
–
Relationships
–
Constraints
Introduction to UML
What’s UML
Goals of UML
Overview
UML: Unified Modeling
Language
The Unified Modeling Language (UML) is
an industry
-
standard language for
specifying, visualizing, constructing, and
documenting the artifacts of software
systems
The UML definition was led by Grady
Booch, Ivar Jacobson, and Jim Rumbaugh
(all now at Rational Software)
In Essence:
a tool used through the analysis and design
phases of system development for expressing the
constructs and relationships of complex systems
Target Usage:
for building object
-
oriented and
component
-
based systems
UML simplifies the process of software
design, making a "blueprint" for construction
1. Provide users with a ready
-
to
-
use, expressive visual modeling
language so they can develop and exchange meaningful models
2. Provide extensibility and specialization mechanisms to extend
the core concepts.
3. Be independent of particular programming languages and
development processes.
4. Provide a formal basis for understanding the modeling
language.
5. Encourage the growth of the Object
-
Oriented tools market.
6. Support higher
-
level development concepts such as
collaborations, frameworks, patterns and components.
7. Integrate best practices.
Goals of UML
Many free learning materials on the web
–
e.g.
www.rational.com/uml
Standard of UML
www.cs.wcupa.edu/~zjiang/01
-
09
-
67.pdf
Some recommended texts on UML:
–
The Unified Modeling Language User Guide
, [G.
Booch, J. Rumbaugh, I. Jacobson, 2000]
–
UML Explained
, [Kendall Scott, 2001]
–
Applying UML and Patterns 2
nd
Ed.
, [Craig Larman,
2002]
–
UML Distilled 2
nd
Ed.
, [Martin Fowler with K. Scott,
2000]
–
UML and C++
, [R. Lee & W. Tepfenhart, 2001]
UML Overview
UML is a language for visualizing,
specifying, constructing and documenting
the artifacts of a software system
The artifacts might include requirements,
architecture, design, source code, project
plans, tests, prototypes, releases
Six diagram types will be introduced: class,
relationship, constraint, activity, sequence,
statechart
Objects and Classes
What’s object
–
Identity
–
State
–
Behavior
Sequence Diagram
Statechart Diagram
Messages and methods
What’s class
–
Objects and Classes
–
Nature of a class
–
Class Attributes
–
Operation (Method)
–
Interfaces
–
Interfaces and Implementation
–
Corresponding C++ code
Objects and Classes
Fundamentals of Object
-
Oriented
Programming
Objects
Conceptually, there are many ways to think
of an object
–
something that can be seen or touched
–
a thing to which some action is directed
–
something that performs an action
The structure and behaviour of similar
objects are defined in their common class
Objects have thee properties: identity
,
state,
and behaviour
Object Property 1:
Identity
Identity is that property of an object which
distinguishes it from all other objects
Most programming languages use variable
names to refer to objects
Keep in mind, however, that an object may
not have a name; Similarly, an object might
have multiple names (aliases)
–
For this reason, there is a subtle distinction
made between the concepts of "name" and
"identity"
Object Property 2: State
The state of an object encompasses all of the
(usually static) properties of the object plus
the current (usually dynamic) values of each
of these properties
Object Property 3:
Behaviour
Behavior is how an object acts and reacts, in
terms of its state changes
The state of an object represents the
cumulative results of its behavior
In object
-
oriented programming, a behavior
is invoked by an interaction to this object
If the receiver object does not have a
method for that message, an error is reported
Example Objects
There are many physical objects we can
examine right in this room
–
each person is an object
–
any chair is not an object
–
each light bulb is an object
–
Any book is not an object
–
this room itself is an object (full or not)
See if the followings are objects or not:
–
Desk
–
Light
–
Person
–
Log
–
The Earth
–
Clock
–
Machine
–
Computer
–
Saving account
Answer: N, Y, Y, N, Y, Y, Y, Y, Y
Objects versus Classes
How would we describe the state, behaviour,
and identity for each of these objects
We have looked at rows as objects and we
have seen that objects can be "classified"
into a table, i.e., class.
As programmers, we work with both classes
and objects
Classes and Objects
An object is called an "instance" of a class
The terms
instance
and
object
are
interchangeable
Creating an object from a class is often
called
instantiation
For example, there are many person objects
in this room
--
each person is an instance of
the person class
The Nature of a Class
A class describes the common structure
(attributes/state) and behaviour of its instances
For example,
–
3.14, 2.71, and 5.5 can be classified as Floats
–
the following shapes can be classified as Circles
In a 2D drawing package, circles have a
radius, a line thickness, a line colour, and a
fill colour
Each individual circle (instance) drawn by
the user has its own value for each attribute
The programmer writes a Circle class and
the program instantiates a Circle object
every time the user draws a Circle
a snowman made from 9
Circle instances
An attribute is a named property of a class
that describes the range of values that
instances of the property may
hold.(Booch,1999)
An attribute has a type that defines the
type of its instances.
Only the object itself should be able to
change the value of its attributes.
The values of the attributes define the
state of the object
Class Attributes
Operation (Methods)
An operation is the implementation of a service
that can be requested from any object of the class
to affect behavior (Booch, 1999)
An operation in database can be:
–
Creation (or adding a new one)
–
Search & read
–
Write / update
–
Closing / deletion
Object
something
action
state1
state2
action
attribute1
attribute2
operation/method
attributes: {attribute1,
attribute2}
operations/methods
value
structure
Review
Light
On
Off
Turn on/off
True
False
Turn_on
attributes: {True,
False}
operations/methods:
value
structure
Turn_off
Turn_on/off ( )
Class Diagram
Introduction
Class Icon
Relationships
Constraints
A game to demonstrate the use of class
diagram
Find your partner
Ask your partner to leave temporarily
–
Look at this picture carefully
–
You need to identify it from VERY similar
pictures in the next slide
–
Which one is shown before?
–
In the next slide, your partner will see the
–
Write down something to help your partner
to pick up the same picture correctly.
–
Quite, your partner’s job to pick up the
selected picture!
–
Which one?
Introduction
The class diagram is fundamental to object
-
oriented programming
UML’s class diagrams capture the attributes
and operations of each class as well the
relationships (information ownership) that
exist between classes
Class Icon
Class Icon
Hiding Details
Visibility Notation
Attribute Specification
Operation Specification
UML Class Icon
The UML class icon is
a rectangle with three
compartments:
–
class name
–
class attributes
–
class operations
Attributes are specified
in the following form:
–
object:class name
Circle
radius: float
center_x: int
center_y: int
area()
display()
Hiding Detail
Circle
area()
display()
Circle
radius: float
center_x: int
center_y: int
Circle
You can optionally leave out the attributes,
operations, or both in a class icon:
UML Class Relationships
A class relationship is a connection between
two (or more) classes
The three most important class relationships
are generalizations, associations, and
aggregations
UML provides a graphical representation for
each of the relationships using a different
line type for each relationship
Class Relationships
Generalization
Association
Association Class
Qualified Association
Ternary Association
Aggregation
Generalization
A generalization is a relationship between a general
thing (superclass) and a more
specific
kind of that
thing (subclass)
In the UML, generalization requires that objects of the
subclass may be used anywhere an object of the
superclass appears
Person
Student
Association
An association is a structural relationship that specifies
that objects of one thing are
connected
to objects of
another
Faculty
Student
Association
Associations can be adorned with a
name
.
Faculty
Student
Teaching
Association
Associations can be adorned with the
roles
.
teacher
Faculty
Student
learner
Association
Associations can be adorned with
the multiplicity
.
4..*
Faculty
Student
1
Association Class (relation
attributes)
Each object of association class is one
instance of relationship (link) in an
association.
1..*
Faculty
Student
4..*
Course
Qualified Association
Qualified association relates two classes and
a qualifier.
The qualifier is a special attribute that
reduces the effective multiplicity of an
association.
Directory
File
file
name
Ternary Association
A ternary association is a structural relationship that
specifies that objects of one thing are
connected
to
objects of other two’s.
Developer
Language
Project
Aggregation
An aggregation is an association that
represents whole/part relationship
The “whole” end of the association
relationship is adorned with an open
diamond shape (e.g. X is part of Y)
e.g. door:Door is part of car:Car
X
Y
Generalization, Aggregation, and
Association
See if the following pairs of classes have
generalization, aggregation or association
Faculty & student (as)
Hospital & doctor (as)
Door & Car (ag
--
<>)
Member & Organization (ag
--
<>)
People & student (ge <|
--
)
Circle & point (ge
--
|>)
Department & Faculty (as)
Employee & Faculty (ge <|
--
)
Item & Printer (ge <|
--
)
Account & Checking account (ge <|
--
)
Constraints
Constraints on Object
Constraints on Relations (Ordering)
General Constraints (Dependency)
Constraints on Object
The constraints restricts the values that
objects can be.
Example: No employee’s salary can exceed
the salary of the employee’s boss.
Employee
Salary
Employer
Salary
{salary <=boss.salary}
General Constraints
(Dependency)
A dependency is a using relationship that states that a
change in specification of one thing may affect another
thing that uses it (but not necessarily the reverse)
General Constraints
(Dependency)
Aerodrome
checkRVR( r: Runway )
Runway
Dependencies are often used when one class uses
another class as an argument of the operation
Dependencies are also often used to express general
constraints.
Person
Committee
member
-
of
chair
-
of
{subset}
Design
•
Organize the data into “something” (i.e.,
object) being queried.
•
Observe the relationship between
objects.
•
Describe the queries into methods,
updating, and even deleting the attribute
information.
•
At the end, translate class, object,
attribute value, and method to table, row,
field value, and query,
resepctively
.
For example, this class!
•
What you think important?
•
What are objects?
•
What are relations?
•
Class?
•
TA?
•
Attributes, and methods?
Mischief
UML class diagram is not object diagram.
How to describe the behaviors/actions of
objects in UML class diagram?
UML class diagram is not data flow chart.
What kind of relationship is generalization?
How to keep the consistency of all the
constraints?
UML class diagram is not object
diagram
Customer
Bank
Loan
Other
-
Loan
•
Should we add relation
between Bank and Other
-
Loans?
•
Can Bank change Other
-
loans?
•
Instance of Loan and Other
-
Loans?
UML class diagram is not object
diagram
Customer
Bank
Loan
Bank
-
Loan
Other
-
Loan
How to describe the
behaviors/actions of objects in
UML class diagram
?
Person
sSN
address
email
Faculty
Student
Customer
Casher
Operation (Methods)
An operation is the implementation of a service
that can be requested from any object of the class
to affect behavior (Booch, 1999)
An operation can be:
–
Question (does not change the value of the object)
–
Command (may change the value of the object)
UML class diagram is not data
flow chart
Customer
Dealer
Product
Manager
buy
sell
report
bargain
authorize
sign
UML class diagram is not data
flow chart
Customer
Dealer
Product
Manager
Salesman
•
Customer, Dealer, and Manager
•
Bargain
What kind of relationship is
generalization
People
Student
x: int
y: int
radius: double
Rectangle
Position
Point
Circle
Position
Point
*Tip 4: Class A is a super class of class B if and only if any
object of class B can also play the role as an object of class A.
How to keep the consistency of
all the constraints
Ticket
-
buyer
Dependent
Traveler
0..6
1
1
1..6
1
1
How to keep the consistency of
all the constraints
Ticket
-
buyer
Dependent
Traveler
0..6
1
0..1
0..1
0..1
1
How to keep the consistency of
all the constraints
Ticket
-
buyer
Dependent
Traveler
1
0..6
0..1
Enter the password to open this PDF file:
File name:
-
File size:
-
Title:
-
Author:
-
Subject:
-
Keywords:
-
Creation Date:
-
Modification Date:
-
Creator:
-
PDF Producer:
-
PDF Version:
-
Page Count:
-
Preparing document for printing…
0%
Σχόλια 0
Συνδεθείτε για να κοινοποιήσετε σχόλιο