31
www.ddss.arch.tue.nl 7M822 UML Class Diagrams 7 October 2010

Www.ddss.arch.tue.nl 7M822 UML Class Diagrams 7 October 2010

Embed Size (px)

Citation preview

www.ddss.arch.tue.nl

7M822

UML Class DiagramsUML Class Diagrams

7 October 2010

www.ddss.arch.tue.nl

7M822

UML Class DiagramsUML Class Diagrams

Class diagrams are used in:• Analysis To build a conceptual domain model with semantic

associations between concepts• Design Structural model of a design in terms of class interfaces• Implementation Source code documentation, exposing the

implementation

The class diagram provides a static structure of all the classes that exist within the system. Classes are arranged in hierarchies sharing common structure and behaviour and are associated with other classes.

www.ddss.arch.tue.nl

7M822

Classes and ObjectsClasses and Objects

• Objects looks like modules in some ways

Object = Identity + State + BehaviourObject = Identity + State + Behaviour• Objects provide encapsulation of data• An object is described by a classobject is described by a class. A class may define a number of

objects with identical properties

www.ddss.arch.tue.nl

7M822

About an ObjectAbout an Object

• An object has a public interface defining the operationsoperations (methods) it will support

• An object has private data called attributesattributes, which only its own operations can access

• An object can have private operations for its own use• An object may know about other objects by means of associationsassociations

www.ddss.arch.tue.nl

7M822

ClassesClasses

A class is simply represented as a box with the name of the class inside– The diagram may also show the attributes and operations

Rectangle Rectangle

heightwidth

Rectangle

heightwidth

getArearesize

Rectangle

height: intwidth: int

getArea(): intresize(int,int)

The complete signature of an operation is: operationName(parameterName: parameterType …): returnType

www.ddss.arch.tue.nl

7M822

www.ddss.arch.tue.nl

7M822

Essentials of UML Class DiagramsEssentials of UML Class Diagrams

The main symbols shown on class diagrams are:• Classes

– Represent the types of data themselves

• Associations– Represent linkages between instances of classes

• Attributes– Are simple data found in classes and their instances

• Operations– Represent the functions performed by the classes and their

instances

• Generalizations– Group classes into inheritance hierarchies

www.ddss.arch.tue.nl

7M822

www.ddss.arch.tue.nl

7M822

Identifying objects ?Identifying objects ?

A possible recipe

– We can identify objects in our problem context by looking for nouns and noun phrases

– Each of these can be underlined and becomes a candidate for an object in our solution

– Eliminate irrelevant objects

• Redundant, vague, event, outside scope, attribute, meta-language

www.ddss.arch.tue.nl

7M822

The Bank Account ExampleThe Bank Account ExampleThe Bank Account ExampleThe Bank Account Example

You are asked to design a system to handle current and savings accounts for a bank. Accounts are assigned to one or more customers, who may make deposits or withdraw money. Each type of account earns interest on the current balance held in it. Current accounts may have negative balances (overdrafts) and then interest is deducted. Rates of interest are different for each type of account. On a savings account, there is a maximum amount that can be withdrawn in one transaction.

Bank employees may check any account that is held at their branch. They are

responsible for invoking the addition of interest and for issuing statements at the correct

times.

A money transfer is a short lived record of an amount which has been debited from one

account and has to be credited to another. A customer may create such a transfer from

their account to any other. Transfers within a branch happen immediately, while those

between branches take three days.

www.ddss.arch.tue.nl

7M822

The Bank Account ExampleThe Bank Account ExampleThe Bank Account ExampleThe Bank Account Example

You are asked to design a system to handle current and savings accounts for a bank. Accounts are assigned to one or more customers, who may make deposits or withdraw money. Each type of account earns interest on the current balance held in it. Current accounts may have negative balances (overdrafts) and then interest is deducted. Rates of interest are different for each type of account. On a savings account, there is a maximum amount that can be withdrawn in one transaction.

Bank employees may check any account that is held at their branch. They are

responsible for invoking the addition of interest and for issuing statements at the correct

times.

A money transfer is a short lived record of an amount which has been debited from one

account and has to be credited to another. A customer may create such a transfer from

their account to any other. Transfers within a branch happen immediately, while those

between branches take three days.

Question ? Recognize the nouns!

www.ddss.arch.tue.nl

7M822

You are asked to design a system to handle current and savings accounts for a bank.

Accounts are assigned to one or more customers, who may make deposits or withdraw

money. Each type of account earns interest on the current balance held in it. Current

accounts may have negative balances (overdrafts) and then interest is deducted. Rates

of interest are different for each type of account. On a savings account, there is a

maximum amount that can be withdrawn in one transaction.

Bank employees may check any account that is held at their branch. They are

responsible for invoking the addition of interest and for issuing statements at the correct

times.

A money transfer is a short lived record of an amount which has been debited from one

account and has to be credited to another. A customer may create such a transfer from

their account to any other. Transfers within a branch happen immediately, while those

between branches take three days.

Nouns in the bank account example

www.ddss.arch.tue.nl

7M822

RedundantRedundant - overdraft, account

VagueVague - amount, money

An event or an operationAn event or an operation - transaction, deposit

Outside scope of systemOutside scope of system - bank, days

An attributeAn attribute - interest, rate of interest, maximum amount, current balance, overdraft

Meta-languageMeta-language - transaction, correct times, record

Nouns eliminatedNouns eliminated

www.ddss.arch.tue.nl

7M822

Nouns leftNouns left

• current account, savings account, customer, branch, statement, transfer

Remark:

The Bank Account is derived from

Pauline Wilcox – ‘The Unified Modelling Language’ in

Msc in Systems Level Integration – Systems Partitioning Module

www.ddss.arch.tue.nl

7M822

Association Adornments: Name, RoleAssociation Adornments: Name, Role• The association has a name - the descriptive term,

often a verb, for the association.

• Each association has two association ends; each end is attached to one of the classes in the association. An end can be explicitly named with a label. This label is called a role name (association ends are often called roles).

Person Company

employee employer

works for

Person Company

employee employer

has employment for

www.ddss.arch.tue.nl

7M822

Association: MultiplicityAssociation: Multiplicity

Multiplicity defines the number of objects associated with an instance of the association.– Default of 1 (1: 1)– 0 or 1: 0..1– Zero or more (0..infinite): *– 1 or more (1..infinite): 1..*– n..m; range from n to m

inclusive

Car Persontransports

passenger

Car Persontransports

passenger

5

Car Persontransports

passenger

*

Car Persontransports

passenger

1..*

Car Persontransports

passenger

2..5

www.ddss.arch.tue.nl

7M822

Association classesAssociation classes

Sometimes an attribute that concerns two associated classes cannot be placed in either of the classes

Student CourseSection* *

Registration

grade

Student CourseSectionRegistration

grade* *

www.ddss.arch.tue.nl

7M822

Attribute AnalysisAttribute Analysis

It is not always clear which attributes belongs to which classes by finding out the class attributes. An attribute is assigned to that class where it is certainly a feature.

For example: a project leader has the attributes name, department and age. But what to do with the attributes project number, project duration, starting time, and budget? These attributes clarifies something about the relation between project leader and type of project.

This often happens if a n:m relation refers to an association between two classes. In that case, we can define a new class, for instance project management.

www.ddss.arch.tue.nl

7M822

-name-department-age

Project leader-project number-starting time-duration-budget

Project

-name-department-age

Project leader-project number-starting time-duration-budget

Project

* *

-starting time-final time

Management

1

*

1

*

www.ddss.arch.tue.nl

7M822

GeneralizationGeneralization

A specialization / generalization relationship, in which objects of the specialized element (child) are substitutable for objects of the generalized element (parent).• Superclass – the generalization of another class, the child.• Subclass – the specialization of another class, the parent.

Customer

Corporate

Customer

Personal

Customer

www.ddss.arch.tue.nl

7M822

Generalization - characteristicsGeneralization - characteristics

• Identify common features concerning behaviour and knowledge. Define these common features on a higher level in the inheritance hierarchy.

• The aim is at behaviour more than knowledge when combining classes.

• Generalization is a bottum-up process.

• A superclass includes all common properties of its subclasses.

www.ddss.arch.tue.nl

7M822

Specialization - characteristicsSpecialization - characteristics

• Define a new class which is a special appearance of an existing class.

• Specialization is a top-down process.

• A subclass can have attributes and operations that are specific for that sub-class.

• A subclass may redefine operations of its super-class.

www.ddss.arch.tue.nl

7M822

www.ddss.arch.tue.nl

7M822

Associations vs. generalizationsAssociations vs. generalizations

Associations describe the relationships that will exist between instances at run time– when you show an instance diagram generated from a class

diagram, there will be an instance of both classes joined by an association

Generalizations describe relationships between classes in class diagrams– they do not appear in instance diagrams at all– an instance of any class should also be considered to be an

instance of that class’s super classes

www.ddss.arch.tue.nl

7M822

AggregationAggregation

Aggregations are special associations that represent ‘part-whole’ relationships– the ‘whole’ side is often called the assembly or the

aggregate

Vehicle VehiclePart*

Country Region*

www.ddss.arch.tue.nl

7M822

CompositionComposition

A composition is a strong kind of aggregation– if the aggregate is destroyed, then the parts are destroyed

as well

Building Room*

www.ddss.arch.tue.nl

7M822

Aggregation and Composition: exampleAggregation and Composition: example

A B

Question: What is OK?1. A2. B3. A & B4. None

www.ddss.arch.tue.nl

7M822

Object diagramObject diagram

An object diagram is shown as a class, and the name is underscored, although an object’s name can be shown optionally preceding the class name as:

objectname: classname.

The object does not have to be named, in which case only the classname is shown underscored.

www.ddss.arch.tue.nl

7M822

Object diagram : exampleObject diagram : example

Computer

name: Stringmemory: String

Author

name: Stringage: Integer

0..* 1..*

www.ddss.arch.tue.nl

7M822

Object diagram : exampleObject diagram : example

Computer

name: Stringmemory: String

Author

name: Stringage: Integer

0..* 1..*

Brian’s PC: Computer

name = “Dell 486”memory = “256MB”

Brian: Author

name = “Brain Jones”age = 35r

Brian’sLaptop: Computer

name = “Toshiba CT”memory = “512MB”

www.ddss.arch.tue.nl

7M822

ReferencesReferences

• Sommerville, Ian (2001)

Software Engineering, 6th edition

http://www.software-engin.com

• Timothy Lethbridge & Robert Laganière (2005)

Object-Oriented Software Engineering, 2nd edition

http://www.lloseng.com

• Martin Fowler (2000, 2004)

Object-Oriented Software Engineering, 2nd edition; 3rd edition