Lecture 04 UML Class Diagram

advertisement
CS3773
Software Engineering
Lecture 04
UML Class Diagram
Class and Object
Objects are discrete entities with well defined boundaries
and encapsulate




2
–
States
–
Behaviors
Classes represent types of objects that share a set of
features: attributes, operations, and relations
Classes describe key concepts involved in the problem
domain
Classes are basic building blocks of OO system
UTSA CS3773
UML Class Diagram Syntax
Elements of class diagram:

–
–
3
Class represented as a box containing three compartments

Name

Attributes

Operations
Relation represented as a line between two classes

Association

Generalization

Aggregation and composition
UTSA CS3773
Class Diagram: Perspectives
UML class diagrams can be used in three distinct ways
depending on the phase of system development:

–
Conceptual (Domain Models)




4
Requirements phase
The diagram represents the problem that the software should
solve
Classes are real-world concepts in the system's environment
Not necessarily a direct mapping to how these will be implemented
(although they may end up being classes in the software system)
UTSA CS3773
Class Diagram: Perspectives
–
Design


–
The diagram depicts only the interfaces of software classes, but
still avoids implementation details (information hiding)
Implementation

5
The diagram is complete: both attributes and operations
The diagram depicts interfaces and implementations of classes
UTSA CS3773
Class
Classes are entities from the problem domain

–
Actors that interact with system
–
Any information that the system stores, analyzes,
transforms, displays, etc.
–
Transient objects
e.g., business transactions, phone conversations, etc.


6
Classes are named, usually, by short singular nouns
Syntax: A box with three compartments for names,
attributes, and operations respectively
UTSA CS3773
Class Diagram – An Example
Class Diagram – An Example
7
UTSA CS3773
Identifying Class
Classes are usually derived from the use cases for the
scenarios currently under development

Brainstorm about all the entities that are relevant to the
system

Noun Phrases

8
–
Go through the use cases and find all the noun phrases
–
Watch out for ambiguities and redundant concepts
UTSA CS3773
Attribute

Attributes are simple data associated with a class
e.g., integer, sets, dates


9
Attributes are properties of a class
Attributes are information that distinguishes one instance
of the class from another instance

They are distinguishing characteristics of the objects

Syntax: name: type = default value
UTSA CS3773
Operation




10
Operations are the responsibilities/services of an object
in the class
Operations query or transform values of object's
attributes, change state
A class's attributes and operations document the purpose
of the class --- what information it maintains, and how
that information can be manipulated
Syntax: name (parameters) : return-type
UTSA CS3773
Static Attribute and Operation

Static attributes model data values shared by all objects
of the class
e.g., number of objects

Static operations are class-related operations not offered
by instances of the class
e.g., “create ()”, “search ()”

11
Syntax: underlined attribute or operation
UTSA CS3773
Association

An association is a relationship between classes

An association is a name, usually short verb
–
Some people name every association
–
Others name associations only when such names will improve
understanding
e.g., avoid names like “is related to”, and “has”

An association represents different types of relationships
e.g., data flow, requests, parts of compound class
12
UTSA CS3773
Association Syntax
An association may have

13
–
An association name
–
Multiplicity
–
Role names
–
Qualifier
–
Association class
–
Navigability
UTSA CS3773
Multiplicity
Multiplicities on associations give lower and upper bound on
the number of instances of the local class that can be linked
to one instance of the remote class

Syntax: 1, *, etc. at the association end. Examples:

14
–
* (zero or more)
–
1 .. * (one or more)
–
1 .. 40 (one to 40)
–
5 (exactly 5)
–
If no multiplicity is specified, the default is 1
UTSA CS3773
Multiplicity

15
Multiplicities on classes indicate how many instances of the
class can be at run time

Multiplicities constrain the number of objects

Syntax: 1, *, etc. at the top right corner of class
UTSA CS3773
Role Name


16
Is a part of association
Describes how the object at the association end is viewed
by the associated object

Is useful for specifying the context of the class

Syntax: name at the association end
UTSA CS3773
Qualified Association




17
Qualified association is an association key that identifies
the object at the other end of the association
Qualifier is a key or index used to identify one or fewer
objects from set of many objects
Qualifier is often an attribute of the class at the other
end of the association, and the attribute is recognized as
uniquely identifying one or fewer objects of the class.
Syntax: name in box at the end of an association
UTSA CS3773
Association Class




18
Association class allows to add attributes, operations to
an association
The association contains more information
Syntax: class connected to the association by a dashed
line
Constraint: only one instance of association class between
any two associated objects
UTSA CS3773
Generalization

Indicates an “is-a” relationship

All instances of the subclass are instances of the super class



19
A subclass inherits all attributes, operations and associations
of the parent
The common attributes and operations are placed in the
superclass; subclasses extend the attributes, operations, and
associations as they need them
Syntax: open triangle at the superclass end of the association
UTSA CS3773
Aggregation

Indicates a loose “has-a” relationship

The compound class is made up of its component classes
Represents the “whole-part” relationship, in which one
object consists of the associated objects


20
–
The whole controls the relationship
–
The part services requests from the whole
Syntax: hollow diamond at the compound class end of the
association
UTSA CS3773
Aggregation Semantics

Whole can exist independently of the parts

Part can exist independently of the whole

Whole is incomplete if some of the parts are missing

21
It is possible to have shared ownership of the parts by
several wholes
UTSA CS3773
Composition


Composition is a particular kind of aggregation
Component classes are physically part of the compound
class
e.g., a car is composed of an engine, wheels, etc.


22
The component class dies if the compound class dies
Syntax: filled diamond at the compound class end of the
association
UTSA CS3773
Composition Semantics




23
Component belong to exactly one compound at a time
Compound is responsible for the creation and destruction
of all its components
Compound may also release its components to other
compounds
If the compound is destroyed, it must either destroy all its
components, or give responsibility for them over to some
other classes
UTSA CS3773
Review of Class Diagram
Class is a set of discrete entities that share the same
features

Class encapsulates data (attributes) and functions
(operations: queries of modifiers)

Multiplicity constrains the number of objects participating
in a relationship at any point in time

Relationship represents connections between classes

24
–
Association: role name, qualifier, association class, navigability
–
Generalization
–
Aggregation and composition
UTSA CS3773
UML Object Diagram

Object is an instance of some class

Object diagram is a snapshot of the objects in a system


25
Object diagram represents all the instances of classes
(objects) and their associations (links) at a point of time
Object diagram gives better feeling of problem by
showing examples
UTSA CS3773
UML Object Diagram Syntax
Object is a box with two compartments: object name and
attributes

Objects name is underlined


26
–
Class name alone
–
Object name alone
–
Object name concatenated with the class name, separated by
a colon
–
Name convention: starting with a lowercase letter
Relations among objects are represented as links
UTSA CS3773
UML Object Diagram – An Example
A: Book
Ann: Patron
: Book1
Jo: Patron
: Book2
Tom: Patron
: Book
27
UTSA CS3773
Reading Assignments
UML 2 and The Unified Process

28
–
Chapter 7, “Objects and classes”
–
Chapter 8, “Finding analysis classes”
–
Chapter 9, “Relationships”
–
Chapter 18, “Refining analysis relationships”
UTSA CS3773
Download