45
UML UML Dr. Zhen Jiang West Chester University E-mail: [email protected]

UML Dr. Zhen Jiang West Chester University E-mail: [email protected]

Embed Size (px)

Citation preview

Page 1: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

UMLUML

Dr. Zhen JiangWest Chester UniversityE-mail: [email protected]

Page 2: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

OutlineOutlineIntroduction to UMLObjects and ClassesClass Diagrams

◦Class Icon◦Relationships◦Constraints

Page 3: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

UML: Unified Modeling UML: Unified Modeling LanguageLanguage

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)

Page 4: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Objects and ClassesObjects and ClassesWhat’s object

◦ Identity◦ State◦ Behavior

Sequence Diagram Statechart Diagram

Messages and methodsWhat’s class

◦ Objects and Classes◦ Nature of a class◦ Class Attributes◦ Operation (Method)◦ Interfaces◦ Interfaces and Implementation◦ Corresponding C++ code

Page 5: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

ObjectsObjectsConceptually, 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 a query action

The structure and behaviour of similar objects are defined in their common class

Objects have thee properties: identity , state, and behaviour (query action)

Page 6: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Object Property 1: Object Property 1: IdentityIdentityIdentity is that property of an object which

distinguishes it from all other objectsKeep 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"

Page 7: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Object Property 2: StateObject 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

Page 8: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Object Property 3: Object Property 3: BehaviourBehaviour

Behaviour is how an object acts and reacts, in terms of its status changes and message/information passing

The state of an object represents the cumulative results of its behaviour

Page 9: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Example ObjectsExample ObjectsThere 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)

Page 10: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

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

Page 11: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Objects versus ClassesObjects versus ClassesHow would we describe the state,

behaviour, and identity for each of these objects

We have looked at objects and we have seen that objects can be "classified" into classes

As programmers, we work with both classes and objects from those classes

Page 12: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Classes and ObjectsClasses and ObjectsAn object is called an "instance" of a classThe terms instance and object are

interchangeableCreating an object from a class is often

called instantiationFor example, there are many person objects

in this room -- each person is an instance of the person class

Page 13: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

The Nature of a ClassThe Nature of a ClassA class describes the common structure

(attributes/state) and behaviour of its instancesFor example,

– 3.14, 2.71, and 5.5 can be classified as Floats

– the following shapes can be classified as Circles

Page 14: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

In a 2D drawing package, circles have a radius, a line thickness, a line color, and a fill color

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

Page 15: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

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 AttributesClass Attributes

Page 16: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Operation (Methods)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)

Page 17: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Objectsomething

action

state1 state2action

attribute1 attribute2

operation/method

attributes: {attribute1, attribute2}

operations/methods

value

structure

ReviewReview

Page 18: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Light

On OffTurn on/off

True FalseTurn_on

attributes: {True, False}

operations/methods:

value

structure

Turn_off

Turn_on/off ( )

Page 19: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Class DiagramClass DiagramIntroductionClass IconRelationshipsConstraints

Page 20: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

IntroductionIntroduction

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 that exist between classes

Page 21: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Class IconClass IconClass IconHiding DetailsVisibility NotationAttribute SpecificationOperation Specification

Page 22: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

UML Class IconUML 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()

Page 23: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Hiding DetailHiding 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:

Page 24: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

UML Member Visibility UML Member Visibility NotationNotation

UML has three visibility prefixes for members:+ for public, # for protected, and – for private

–e.g.Circle

-radius: float

#area()

+display()

Page 25: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Full UML Attribute Full UML Attribute SpecificationSpecification

The full form of a UML attribute is as follows:[visibility] name [multiplicity] [: type] [= initial value] [{property}]

– The property choices are changeable, addOnly, and frozen

Student Info

-id: string

-hasGraduated: bool = false

Page 26: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Full UML Operation Full UML Operation SpecificationSpecification

The full form of a UML operation is as follows:[visibility] name [(parameter-list)] [:return-type] [{property}]

– The property choices are sequential, concurrent, guarded, and isQuery

The full form of a UML parameter is:[direction] name : type [= default-value]

– The direction choices are in, out, and inout

Page 27: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

UML Class RelationshipsUML Class RelationshipsA class relationship is a connection between

two (or more) classesThe 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

Page 28: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Class RelationshipsClass RelationshipsGeneralizationAssociationAssociation ClassQualified AssociationTernary AssociationAggregation

Page 29: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

GeneralizationGeneralization 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

Page 30: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

AssociationAssociationAn association is a structural relationship that

specifies that objects of one thing are connected to objects of another

Faculty Student

Page 31: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

AssociationAssociationAssociations can be adorned with a name.

Faculty StudentTeaching

Page 32: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

AssociationAssociationAssociations can be adorned with the roles.

teacherFaculty Studentlearner

Page 33: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

AssociationAssociationAssociations can be adorned with the multiplicity.

4..*Faculty Student

1

Page 34: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Association Class (relation Association Class (relation attributes)attributes)

Each object of association class is one instance of relationship (link) in an association.

1..*Faculty Student4..*

Course

Page 35: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Qualified AssociationQualified Association

Qualified association relates two classes and a qualifier.

The qualifier is a special attribute that reduces the effective multiplicity of an association.

Directory Filefile name

Page 36: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Ternary AssociationTernary Association A ternary association is a structural relationship that

specifies that objects of one thing are connected to objects of other two’s.

Developer

LanguageProject

Page 37: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

AggregationAggregation

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

Page 38: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Generalization, Aggregation, and Generalization, Aggregation, and AssociationAssociation

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 <|-- )

Page 39: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

ConstraintsConstraintsConstraints on ObjectConstraints on Relations

(Ordering)General Constraints (Dependency)

Page 40: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Constraints on ObjectConstraints on ObjectThe 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}

Page 41: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

Constraints on Relations Constraints on Relations (Ordering)(Ordering)

{Order} indicates that the elements of the “many” end of an association have an explicit order that must be preserved.

Paper Author{ordered}

11..*

Page 42: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

General Constraints General Constraints (Dependency)(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)

Page 43: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

General Constraints General Constraints (Dependency)(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 Committeemember-of

chair-of

{subset}

Page 44: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

(Class Name, attribute, method) vs (Table Name, field, query)

(Class & object) vs (Table & row) (Table 1, table 2, … ) vs Knowledge relearning with

OO techniques to quickly locate the information

Page 45: UML Dr. Zhen Jiang West Chester University E-mail: zjiang@wcupa.edu

DesignDesign• Organize the data into “something” (i.e., object) being queried.• Observe the relationship between objects.• Describe the queries into methods, changing, updating, and even deleting the attribute information.• If necessary, translate class, object, attribute value, and method to table, row, field value, and query.