UML Class Diagrams. Lesson Objectives

Full text

(1)

1

UML Class Diagrams

Lesson Objectives

Understand UML class diagrams and object modellingBe able to identify the components needed to produce

a class diagram from a specification

Be able to produce class diagrams and document them using Eclipse/Together

(2)

3

UML Class

BankAccount

balance

iRateCharge

deposit

withdraw

Name

Attributes

Methods/

operations

4

UML Class: Different levels of detail

BankAccount - balance: int - iRateCharge: float +deposit: int +withdraw: int signature BankAccount balance iRateCharge deposit withdraw BankAccount balance iRateCharge BankAccount

(3)

5

Objects and Classes

ClassName

attribute1

attribute2: data_type attribute3: type = init_val ... operation1() operation2(arg_list) operation3() : returntype ... anObject: ClassName attribute1 = value1 attribute2 = value2

Object (instance) ..…is instantiated from ……... Class

Objects and Classes: Example

Car

manufacturer

registrationNumber: Integer driver: String = “Unspecified” ...

register()

drive (Integer speed) getDriver() : String ... spaceWagon: Car manufacturer = Mitsubishi registrationNumber = R637GNU driver = “Naomi”

(4)

9

Class Diagrams

BankAccount Customer 10

Associations: Multiplicity

A student takes between 0 and 12 modulesA module is taken by at least 10 students

Student 10..* Module 0..12 • Many-to-many relationship Student * Module *

(5)

11

1-to-1 and 1-to-many Associations

1-to-1 association 1-to-many association * draw() Polygon x:Integer y:Integer Point 1 Has-capital name:String Country name:String City 1 1

Association Notation

married to wife husband 0..1 0..1 0..* 0..1 parentage offspring 0..1 0..1 husband wife married to Individual Family child

(6)

13

Aggregation

An aggregationis a special case of association denoting a “consists of” hierarchy.

The aggregateis the parent class, the componentsare the children class.

1

Exhaust System

Muffler Tailpipe 0..2

“is a part of”

14 weight Car serial number year manufacturer model colour drive purchase

Aggregation Example

Engine horsepower volume on off 3,4,5 Wheel diameter number of bolts 2,4 Door open close Battery amps volts charge discharge * Brakelight on off 1 1

(7)

15

Composition

A solid diamond denotes composition, a strong form of aggregation where components cannot exist without the aggregate.

3 TicketMachine

ZoneButton

Assembly or Composition?

Assembly: component can exist on its own

Composition: component cannot exist on its own

*

Football Club

(8)

17

Generalization/Inheritance

SavingsAccount Withdraw() ChequeAccount Withdraw() Account Balance AccountId Deposit() Withdraw() GetBalance() Is a kind of 18

Inheritance

Classes inherit the attributes and operations of their ‘parents’ i.e. from the generalization to the specialization

Operations and attributes may be re-defined

Additional operations or attributes must be definedOperations and attributes may notbe removed in

(9)

19

Associations: which type?

If in doubt use simple association

Use aggregation or composition for “has a” relationships

Use inheritance for “is a” relationships

Visibility and Scope ...

Public ...………. +

Visible to using classes

Protected ……….. #

Visible to subclasses

Private ………...

-Visible only within this class

Window -size : Rectangle -xptr : Xwindow * -visibility : Boolean +Window () +display () : void

(10)

21

Finding Classes

Study use case descriptions

Grammatical analysis Nouns AdjectivesVerbsBrainstormUse CRC cards 22

Example: Scenario From Problem

Statement

Jim enters a store with the intention of buying a toy for his 3 year old child.

Help must be available within less than one minute. The store owner gives advice to the customer. The

advice depends on the age range of the child and the attributes of the toy.

Jim selects a dangerous toy which is unsuitable for the child.

The store owner recommends a more suitable toy -a doll.

(11)

23

Mapping Parts of Speech to Object

Model Components

Part of speech Model component Example

Proper noun object Jim Smith

Improper noun class Toy, doll

Doing verb method Buy, recommend

being verb inheritance is-a (kind-of)

having verb aggregation has an

modal verb constraint must be

adjective attribute 3 years old

transitive verb method enter

intransitive verb method (event) depends on

Identifying Inappropriate Classes

Redundant

Vague

An event or an operationMeta-language

Outside the scope of the system An attribute

(12)

25

CRC Card

26

Two OO Design Camps

Data driven design

(13)

27

Package It All Up

Pack up design into discrete physical units that can be edited, compiled, linked, reused

Construct physical modules

Ideally use one package for each subsystem

System decomposition might not be good for implementation

Two design principles for packaging

Minimize coupling Maximize cohesiveness

UML Packages

General mechanism for grouping modeling elements

Package “owns” its contentsDefines a nested name space

e.g. PackageName::ClassName

c.f. Java Style: PackageName.ClassName

Elements belong to one and only onePackage Packages can be nested.

(14)

30

Packages with Dependencies

Dependencies should be one-way

Avoid cyclic dependencies!

Less stable components depend on more stable (reusable) ones

Packages reflect

development responsibility

The art of large scale design is minimising dependencies.

32

Summary

Class diagrams are the central feature of UMLClass diagrams enable us to model objects,

classes and associations between classes. At more detailed level you may need to consider

special types of association like aggregation and composition

Inheritance is an especially important type of association

Finding classes is difficult - techniques like noun identification and CRC cards are helpful

Figure

Updating...

References

Updating...

Related subjects :