advanced uml class diagrams

31
1 © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Advanced UML Class Advanced UML Class Diagrams Diagrams

Upload: chaney

Post on 07-Jan-2016

40 views

Category:

Documents


2 download

DESCRIPTION

Advanced UML Class Diagrams. Objectives. To present the portions of the UML class diagram notation not already covered To present examples to illustrate the use of the notation To present heuristics for using the notation well. Topics. Generalization - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Advanced UML Class Diagrams

1© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Advanced UML Class Advanced UML Class DiagramsDiagrams

Page 2: Advanced UML Class Diagrams

2© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

ObjectivesObjectives

To present the portions of the UML class diagram notation not already covered

To present examples to illustrate the use of the notation

To present heuristics for using the notation well

Page 3: Advanced UML Class Diagrams

3© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

TopicsTopics

Generalization Abstract and concrete operations and classes Interfaces Feature visibility Class and instance variables and operations Aggregation and composition Association classes and qualifiers Other association adornments More class diagram heuristics

Page 4: Advanced UML Class Diagrams

4© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

GeneralizationGeneralization

Generalization is the UML relation that holds between one model element (the parent) and another (the child) when the child is a special type of the parent.

• Represented by a hollow rectangle and lines

• Rectangle attaches to the parent and lines to the children

Generalization is used in UML class diagrams to model inheritance.

Page 5: Advanced UML Class Diagrams

5© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Generalization ExampleGeneralization Example

Mammal

PumaTiger

Feline

Quadruped

Lion

Lizard

Page 6: Advanced UML Class Diagrams

6© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Generalization versus Generalization versus AssociationAssociation

Generalization is a relation between classes.

Associations represent relations on sets of class instances designated by the associated classes.

Generalization is not a kind of association. They

• Never have multiplicities,• Never have rolenames,• Never have names (they already have a

name: generalization).

Page 7: Advanced UML Class Diagrams

7© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Abstract Operations and Abstract Operations and ClassesClasses

An abstract operation is an operation without a body; a concrete operation has a body.

An abstract class is a class that cannot be instantiated; a concrete class can be instantiated.

A class • Must be abstract if it has an abstract

operation;• May be abstract even if it has no abstract

operations.

Page 8: Advanced UML Class Diagrams

8© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Using and Representing Using and Representing Abstract Classes and Abstract Classes and OperationsOperations

Abstract classes force their subclasses to implement certain operations.

Abstract classes are represented in UML by

• Italicizing their names,• Stereotyping them «abstract» or• Giving them an {abstract} property.

Abstract operations are represented in UML by

• Italicizing their specification or• Giving them an {abstract} property.

Page 9: Advanced UML Class Diagrams

9© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Abstract Class and Abstract Class and Operation ExamplesOperation Examples

Circle ScreenRenderer Book

Document{ abstract }

callNumberdescriptors : String[*]render()

Renderer«abstract»

Shape

areaOf() : float { abstract }

Page 10: Advanced UML Class Diagrams

10© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

InterfacesInterfaces

A UML interface is named collection of public attributes and abstract operations.

• Provided interfaces—realized by a class or component and represented by A ball (lollipop) symbol or A stereotyped class icon with a realization

connector

• Required interfaces—needed by a class or component and represented by A socket symbol or A dependency arrow to a ball symbol or A dependency arrow to a stereotyped class icon

Page 11: Advanced UML Class Diagrams

11© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Provided Interface Provided Interface NotationsNotations

TimerObserver

Observer

TimerObserver

«interface»Observer

update( arg : Object )

Page 12: Advanced UML Class Diagrams

12© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Required Interface Required Interface NotationsNotations

Observer

TimerObserver

«interface»Observer

update( arg : Object )

TimerTimer

Observer

Timer

Page 13: Advanced UML Class Diagrams

13© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Module Assembly Module Assembly NotationsNotations

«interface»Observer

update( arg : Object )

Timer

Observer

Timer

TimerObserverTimerObserver

Page 14: Advanced UML Class Diagrams

14© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

UML Feature VisibilityUML Feature Visibility

Public—Visible anywhere that the class in which it appears is visible; denoted by +.

Package—Visible anywhere in the package containing the class in which it appears; denoted by ~.

Protected—Visible in the class in which it appears and all its sub-classes; denoted by #.

Private—Visible only in the class in which it appears; denoted by -.

Page 15: Advanced UML Class Diagrams

15© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Feature Visibility Feature Visibility ExampleExample

Alarm

+ identifier : String {constant}# location : String# description : String- classification : Classifier

+ getLocation() : String+ getDescription() : String+ action()~ setClassification( c : Classifier)~ getClassification() : Classifier# basicAction()

FireAlarm IntrusionAlarm FloodAlarm

Page 16: Advanced UML Class Diagrams

16© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class and Instance Class and Instance Variables and Variables and OperationsOperations

An instance variable is an attribute whose value is stored by each instance of a class.

A class variable is an attribute whose value is stored only once and shared by all instances.

An instance operation must be called through an instance.

A class operation may be called through the class.

In UML class variables and operations are called static.

• Indicated by underlining an attribute’s or operation’s specification

Page 17: Advanced UML Class Diagrams

17© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Attribute and Operation Attribute and Operation Specification ExamplesSpecification Examples

Tokenizer

+ token : TokenType = NONE+ tokenText : String- buffer : String- bufferPosition : integer- delimiters : String

- theInstance : Tokenizer

+ getInstance() : Tokenizer+ nextToken()+ setBuffer( buffer : String )+ setDelimiter( delim : String )

TokenType

+ NONE : TokenType { constant }+ NUMBER : TokenType { constant }+ IDENTIFIER : TokenType { constant }+ EQUAL : TokenType { constant }+ MINUS : TokenType { constant }+ PLUS : TokenType { constant }- name : String

- TokenType( name : String )+ toString() : String

Page 18: Advanced UML Class Diagrams

18© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Aggregation and Aggregation and CompositionComposition

The aggregation association represents the part-whole relation between classes.

• Denoted by a solid diamond and lines• Diamond attaches to the aggregate (whole)

while lines attach to the parts• May have all association adornments

The composition association is an aggregation association in which each part can be related to only one whole at a time.

• Denoted by a hollow diamond and lines

Page 19: Advanced UML Class Diagrams

19© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Aggregation and Aggregation and Composition ExamplesComposition Examples

Human

FamilyChurch

Person

FootHand

LegArm

0..2 0..2

0..1 0..1

*

1..*

1..*

1..*

Page 20: Advanced UML Class Diagrams

20© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Problems with Problems with AggregationAggregation

Often abused• Must be transitive:

(x)(y)(z)((Rxy • Ryz) Rxz)

• Often used for collection membership, which is not transitive

Aggregation, composition, and collection membership can be represented by plain associations.

Page 21: Advanced UML Class Diagrams

21© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Association ClassesAssociation Classes

An association class represents a relation on instances of the classes is connects, and it holds data and behavior.

• Association class connector is a dashed line• Connects class with an association line

There can be only one instance of an association class for each pair of instances of the associated classes.

If more instances are need, interpose a new class (a reified association).

Page 22: Advanced UML Class Diagrams

22© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Association Class Association Class ExampleExample

Medication Patient* *

Prescription

- date

Page 23: Advanced UML Class Diagrams

23© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Association QualifiersAssociation Qualifiers

An association qualifier is one or more association attributes that, together with instances of the qualified class, pick out instances of an associated target class.

• Represented by a box with association attributes and a line

• Attributes have standard specifications except that no initial values are allowed

• Box is attached to the qualified class• Line runs to the target class• Line can have association adornments

Page 24: Advanced UML Class Diagrams

24© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Association Qualifier Association Qualifier ExamplesExamples

Catalog

Course

departmentnumber

List

Object

index:int

0..1 0..1

1..**

Page 25: Advanced UML Class Diagrams

25© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rolename VisibilityRolename Visibility

Associations are often implemented using references.

• Rolename may be a reference attribute name

Rolenames may have visibility modifiers (+, #, ~, -).

Rolename visibility modifiers may be suppressed.

Page 26: Advanced UML Class Diagrams

26© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Association NavigabilityAssociation Navigability

An association between classes A and B is navigable from A to B if an instance of A can access an instance of B; otherwise it is non-navigable.

• Navigability is denoted by adding an arrowhead to the association line in the navigation direction

• Non-navigability is denoted by placing an x on the association line in the navigation direction

• Navigability markers can be suppressed, so no markers means the diagram is silent on navigability

Page 27: Advanced UML Class Diagrams

27© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Rolename Visibility and Rolename Visibility and Navigability ExampleNavigability Example

Game

Player

Referee

- player1..*

*

0..1 - myGame

1

Page 28: Advanced UML Class Diagrams

28© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class Diagram Class Diagram Heuristics 1Heuristics 1

Never place a name, rolenames, or multiplicities on a generalization connector.

Use the «abstract» stereotype and {abstract} property to indicate abstract classes and operations when drawing diagrams by hand; use italics when drawing diagrams on the computer.

Use the interface ball and socket symbols to abstract interface details and a stereotyped class symbol to show details.

Page 29: Advanced UML Class Diagrams

29© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class Diagram Class Diagram Heuristics 2Heuristics 2

Don’t italicize interface or operation names.

Show provided interfaces with the interface ball symbol or the stereotyped class symbol and a realization connector.

Show required interfaces with the interface socket symbol or dependency arrows to stereotyped class symbols or interface ball symbols.

Avoid aggregation and composition.

Page 30: Advanced UML Class Diagrams

30© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Class Diagram Class Diagram Heuristics 3Heuristics 3

If aggregation or composition is used, check that the represented relation is transitive.

Use association classes when only a single association class instance is associated with each pair of instances of the associated classes; otherwise use a reified association.

Determine multiplicities involving a qualifier by considering qualified class instances and particular combinations of qualifier attribute values.

Page 31: Advanced UML Class Diagrams

31© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

SummarySummary

UML class diagrams have great expressive power: they are able to show

• Generalization• Abstract operations and classes• Class and instance variables and operations• Interfaces and interface assemblies• Aggregation and composition associations• Association classes and quantifiers• Feature and rolename visibility• Navigability