3. UML Object Models

3. UML Object Models 3.1. Introduction UML Object models capture the static structure of a system, as a: •  class architecture, or •  static object st...
12 downloads 0 Views 148KB Size
3. UML Object Models 3.1. Introduction UML Object models capture the static structure of a system, as a: •  class architecture, or •  static object structure at some time instant. Both are represented graphically.

3.2. UML Class diagrams Class architectures lead to UML class diagrams which show: •  the template structure of a class, •  inheritance relations between classes, •  other relations between classes.

A UML class diagram contains 3 kinds of information: (1)  The attributes and methods of a class, perhaps with visibility constraints, typing information and initialisation values. (2) UML defined relations between classes, mainly: (a) is_a and, (b) has_a (shared or unshared) with multiplicity (1-to-1, many-to-1, etc) (3)  User defined relations between classes, e.g. x father_of y, with multiplicity

3.2.1. Class Attributes A class definition provides a template for creating objects. This information includes: •  a class name, •  names and types of attributes, •  names and types of methods, •  initialization values at object creation time, •  visibility attributes of members.

UML class name Employee -  identity # status - pay

: String : int : float

+ fire() : void + promote( increment : int ) : void

UML visibility

UML attributes

UML methods

Optionally can mention code inside a comment

UML Stereotypes include: , and

Visibility

- : private +: public #: protected ~: package

A class attribute has the general format [ : ] [ = ] where the information in brackets [ …] is optional.

UML 2.0 allows C++ style grouping public

a(): int



b(): void private

c():void UML 2.0 allows properties {readOnly}, {ordered},{bag}, {sequence}, {composite}, {union} e.g. area : int {readOnly}

3.2.2. Inheritance Information After individual classes have been identified by analysis as Nouns we try to identify inheritance relationships, e.g. subtypes

employee ≥ office manager employee ≥ office worker

This will help us to: •  re-use code •  refactor designs and code •  clarify definitions –  is every x really a y ? –  what’s the difference between x and y ?

•  understand the structure of the domain •  spot incomplete models ( x ≥ ??? ≥ z )

A UML inheritance hierarchy Class_B

Class_A

multiple inheritance

Class_E

Class_C

Class_D

Class_F

single inheritance

Important Note: no multiplicity!!!

class_C and class_D inherit from class_A. class_C also inherits from class_B (multiple inheritance, in C++ but not Java!) Also class_E and class_F inherit from Class_C. Tip don’t overextend class hierarchies, Max Depth

Suggest Documents