45
2003 SJSU -- CmpE L3-S1 Class Diagrams Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San José State University One Washington Square San José, CA 95192-0180 http://www.engr.sjsu.edu/~fayad

L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

  • View
    221

  • Download
    2

Embed Size (px)

Citation preview

Page 1: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU -- CmpE L3-S1 Class Diagrams

Database Design

Dr. M.E. Fayad, Professor

Computer Engineering Department, Room #283I

College of Engineering

San José State University

One Washington Square

San José, CA 95192-0180

http://www.engr.sjsu.edu/~fayad

Page 2: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S2 Class Diagrams

2

Lesson 9:Class Diagrams

Page 3: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S3 Class Diagrams

Lesson Objectives

Objectives

3

Explore Modeling Notation Understand Objects, Attributes, Operations, and Notes in UML

Discuss the following:

– Different Kind of Classes

– Class Diagrams

Page 4: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S4 Class Diagrams

Notation

Notation Again !

Modeling Notation

4

Page 5: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S5 Class Diagrams

Real-world systems can be decomposed into discrete

entities called objects

Objects represent things, concepts, or abstraction with

definable boundaries and behaviors

An object is specific instance of a class to which it

belongs

Classes have attributes and behaviors

Individual objects have their own specific values for each

attribute and share the same behaviors

Objects & Classes (1)

5

Page 6: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S6 Class Diagrams

Example Objects Classes Attributes

A computer screen Screen Resolution, # of colorsA window Window Size, location

IBM Company Name, location, total revenue

Joe Harris Employee Name, department, salary

Objects & Classes (2)

6

Page 7: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S7 Class Diagrams

Objects & Classes (3)

Objects with the same attributes, behaviors, and relationships

are grouped together into classes

A class describes general attributes and behaviors for a group of

objects

An object is a single instance of its class (e.g., IBM is an

instance of the Company class)

A Class diagram is used to describe the attributes and behaviors

of a class

7

Page 8: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S8 Class Diagrams

Classes (1)

Classes are the most important building block of any Classes are the most important building block of any

object-oriented systemobject-oriented system

A class is a description of a set of objects that share A class is a description of a set of objects that share

the same attributes, operations, relationships, and the same attributes, operations, relationships, and

semantics.semantics.

Graphically, a class is rendered as a rectangleGraphically, a class is rendered as a rectangle

8

Page 9: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S9 Class Diagrams

Classes (2)

9

RegistrationForm

RegistrationManager

Course

Student

CourseOfferingProfessor

ScheduleAlgorithm

Page 10: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S10 Class Diagrams

Naming & AttributesNaming & Attributes NamingNaming

Classes should be named using the vocabulary of the domainClasses should be named using the vocabulary of the domain

Naming standards should be created -- e.g., all classes are Naming standards should be created -- e.g., all classes are

singular nouns starting with a capital lettersingular nouns starting with a capital letter

AttributesAttributes

An attribute is a named property of a class that describes a An attribute is a named property of a class that describes a

range of values that instances of the property may holdrange of values that instances of the property may hold

10

Page 11: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S11 Class Diagrams

Attributes

11

Each course offeringhas a number, location and time

CourseOffering

numberlocationtime

Page 12: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S12 Class Diagrams

OperationsOperations

An operation is the implementation of a service that An operation is the implementation of a service that

can be requested from any object of the class to affect can be requested from any object of the class to affect

behaviorbehavior

12

RegistrationManager

addCourse(Student,Course)

Page 13: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S13 Class Diagrams

Finding Classes

Where to find candidate classes:Where to find candidate classes:

o Nouns in descriptions or conversationNouns in descriptions or conversation

o Checklists of general/typical object typesChecklists of general/typical object types

o Similar computerized systemsSimilar computerized systems

o Technical literature in the problem domainTechnical literature in the problem domain

13

Page 14: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S14 Class Diagrams

Example Classes

14

Phenomena Classes

Thing Car, goods, packaging, materials

People & roles Employee, parent, customer, member

Organizations Company, department, group, project

Places Shelf, parking spot, construction site, city

Concepts Square, currency, quality parameters, fee

Resources Money, time, energy, labor force, info

Apparatuses Radar, sensor, valve, motor

Systems Street register, cash register, alarm system

Page 15: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S15 Class Diagrams

Evaluating Classes

Does the class contain unique information?Does the class contain unique information?

Does the class encompass multiple objects?Does the class encompass multiple objects?

Can we identify objects from the class?Can we identify objects from the class?

Does the class have a manageable number of Does the class have a manageable number of

operations?operations?

Does the class embodies a well-defined responsibility?Does the class embodies a well-defined responsibility?

15

Page 16: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S16 Class Diagrams

16

Classes in UML (1)

Class AbstractClass

<<utility>>UtilityClass

attribute

operation()

<<Stereotype>>Package::Class{Constraints}

Syntax for attributes and operationsAttribute[Cardinality]:Package::Type = InitialValue {Constraints}Operation(ArgumentList):ReturnType {Constraints}

<<metaclass>>MetaClass

<<ActiveClass>>ActiveClass

Page 17: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S17 Class Diagrams

17

Classes in UML (2)

ParameterizedClass

ParameterizedClass<Parameter>

i:Element

normalOperation()abstractOperation()/derivedOperation()

classOperation()publicOperation()

#protectedOperation()-privateOperation()

Class

normalAttribute/derivedAttribute

classAttributepublicAttribute

#protectedAttribute-privateAttribute

Class

Page 18: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S18 Class Diagrams

Related Terms: Type

Definition: Class = Attributes + Operations

A class includes the definition of potential constraints, tagged values, and stereotypes.

Notation:

18

Classes in UML (3)

Class

attribute1attribute2

operation1()operation2()

Class

attribute1attribute2

Class

attribute1attribute2

Class

Class

operation1()operation2()

operation1()operation2()

Class

Page 19: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S19 Class Diagrams

<< .. >> stereotype

{ .. } tagged values

:: separate package and class names

19

Classes in UML (4)

attribute: Type=InitialValue {Constraints}

operation(Paramter) {Constraints}

<<Stereotype>>Package::Class

{PropertyValues}

Page 20: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S20 Class Diagrams

20

Classes in UML (5)

Circle

Class name

Attribute names

Attribute type

Operations

Class Example

Constraint

Initial value

Parameters(name: type = initial value)

radius {radius>0}centerpoint: Point = (10, 10)

display(0remove()

setPosition(pos: Point)setRadius(newRadius)

Graphics::Circle

Package name

Page 21: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S21 Class Diagrams

21

Objects in UML (1)

Object : Class

attributeName = value

Object

:Class

ActiveObject

Multiobject

Page 22: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S22 Class Diagrams

22

Objects in UML (2)

Related terms: instance Definition:

– An object is a unit which actually exists and acts in the current system.

– Each object is an instance of a class.

– An object contains information represented by the attributes whose structure is defined in the class.

– An object can receive the messages defined in the class, that is, it has appropriate operations for each message defined.

Page 23: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S23 Class Diagrams

23

Objects in UML (3)

Description:

– An alternative term for object is instance.

– A class contains the definition of objects, that is, their abstract description.

– The behavior of an object is described through the possible messages it can understand.

– For each message, the object needs appropriate operations.

Page 24: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S24 Class Diagrams

24

Objects in UML (4)

Notation– Objects are represented by rectangles which either

bear only their own name, or which in addition show the name of their class, or the values of specific or all attributes.

– If attribute values are indicated, the rectangle is divided into two sections, separated by a horizontal line.

– The name of the object is underlined, and usually begins with a lower case letter.

Page 25: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S25 Class Diagrams

25

Objects in UML (5)

Example:

aCircle: Circle

Radius = 25Center = (10,10)

Instance name

Attribute names

Class name

Attribute values

Page 26: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S26 Class Diagrams

26

Attributes in UML (1)

Related terms: data element, instance variable, variable, member

Definition:– An attribute is a (data) element which is

contained in the same way in each object of a class and is represented by each object with an individual value.

Page 27: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S27 Class Diagrams

27

Attributes in UML (2)

Description: Each attribute is at least described by its name. In addition, a

data type or a class, plus an initial value and constraints may be defined.

Constraints can be used in addition to the type specification to further restrict the value range or value set of the attribute, or to make it dependent on other conditions.

Tagged values can be used to specify additional special properties. Thus, for example, the tagged value {readonly} indicates that an attribute may only be read.

Page 28: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S28 Class Diagrams

28

Attributes in UML (3)

Notation:– Attribute names begin with lower-case characters and class

names with upper-case one, while tagged values and constraints are enclosed in braces

• attribute : Package::Class =

InitialValue {PropertyValue} {Constrant}

– Derived attributes are marked by a prefixed slash (/)

/derivedAttribute

classAttribute

+publicAttribute

#protectedAttribute

-privateAttribute

Page 29: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S29 Class Diagrams

29

Attributes in UML (4)

Examples:

name: String = ‘Unknown’

invoiceDate : Date = today

birthDate : Date

color : {red, blue, green}

radius : Integer = 25 {readonly} {radius>0}

/numChildren {numChildren = childrenSet count}

/age {age = today - birthDate}

-counter : Integer

dynamicArray[*]

Page 30: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S30 Class Diagrams

30

Operations, Methods in UML (1)

Related terms: method, service procedure, routine function, message

Definitions:– Operations are services which may be required from an object.

– They are described by their signature (operation name, parameters, and if needed, return type).

– A method implements an operation; it is a sequence of instructions.

– A message passes an object the information on the activity it is expected to carry out, thus requesting it to perform an operation.

Page 31: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S31 Class Diagrams

Description– A message consists of a selector (a name) and a

list of arguments, and is directed to exactly one receiver.

– The sender of a message is as a rule returned exactly one response object.

– Inside a class definition, an operation has a unique signature composed of the name of the operation, potential parameters, and a potential return value (function result).

31

Operations, Methods in UML (2)

Page 32: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S32 Class Diagrams

Description (continued)– Operations may be provided with constraints which

can describe the conditions to be met at the call or the values the arguments may have, among other things.

– Tagged values can be used to describe additional special features. Some tagged values are:

• {abstract} to indicate an abstract operation

• {obsolete} to indicate that this operation exists only for compatibility with previous versions.

32

Operations, Methods in UML (3)

Page 33: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S33 Class Diagrams

Notation– The signature of an operation is given as

follows:• name(argument : ArgumentType = DefaultValue, …):

ReturnType {PropertyValues} {Constraints}

– Example:• setPosition(x : Integer = 1, y : Integer =1):

Boolean {abstract} {(x > 0) and (y > 0)}

33

Operations, Methods in UML (4)

Page 34: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S34 Class Diagrams

Naming Conventions– Be extremely careful with the naming of

operations. You should be conscious of what the operation is supposed to do and for which outcomes it is responsible.

– Always try to use active verbs, be careful with adjectives, and be precise!

34

Operations, Methods in UML (5)

Page 35: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S35 Class Diagrams

Related terms: restriction, integrity rule, condition, tagged value, stereotype, not dependency, invariant, assertion.

Definition:– A constraint is an expression which restricts the

possible contents, states or the semantics of a model element and which must always be satisfied.

35

Constraints in UML (1)

Page 36: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S36 Class Diagrams

Description:– A constraint describes a condition or integrity

rule.

Notation:– Constraints are enclosed in braces:

• { Constraint }

36

Constraints in UML (2)

Page 37: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S37 Class Diagrams

Dependency

37

Constraints’ Examples in UML (3)

Project Employee{subset} 11..*

has

consists of

projectLeader

projectMembers

Project Employee11..*

has

consists of

projectLeader

projectMembers

Project self.projectMembers -> includes (self.projectLeader)

Page 38: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S38 Class Diagrams

Consistency

38

Constraints’ Examples in UML (4)

Customer

Contract

1

0..*

has

receives

based on

Invoice self.contract.customer = self.customer

Invoice

1

1

0..*

Page 39: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S39 Class Diagrams

OR

39

Constraints’ Examples in UML (5)

Person

Domesticaddress

{or}

*

*

has

Foreignaddress

has

Page 40: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S40 Class Diagrams

Values

40

Constraints’ Examples in UML (6)

Rectangle

a {a > 0}b {b > 0}

Triangle

a {c-b < a < b+c}b {a-c < b < a+c}c {a-b < c < a+b}

Page 41: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S41 Class Diagrams

Order

41

Constraints’ Examples in UML (7)

NameList1..*contains

{ordered: lastName}

1 Person

lastNamefirstName

Page 42: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S42 Class Diagrams

Formulas

42

Constraints’ Examples in UML (8)

Person

birthDate : Date/age {age = today - birthDate}

{/age = today - birthDate}

Person

birthDate : Date/age

Page 43: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S43 Class Diagrams

Enumerations

43

Constraints’ Examples in UML (9)

ShoppingSpree1..*

paricipants

{participant.age > 65}1 Person

age

Page 44: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S44 Class Diagrams

44

Notes in UML (1)

Related Terms: annotation, comment

Definition: Notes are comments to a diagram or an arbitrary element in a diagram, without any semantic effect.

Description: notes are annotations to classes, attributes, operations, relationships, and the like.

Some analysis and design tools provide the possibility of creating notes with user-defined structures and names

Page 45: L3-S1Class Diagrams 2003 SJSU -- CmpE Database Design Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering San

2003 SJSU – CmpE M.E. Fayad L3-S45 Class Diagrams

45

Notes in UML (2)

Note Notation & Example:

Invoice

invPositions : Set

sumPositions()sumVAT()

InvPosition

quantityitem

unitPrice

totalPrice()

*1

sumPositions “This example is in Smalltalk” “Return total amount of all invoice positions” | s | s:= 0. invPositions do: [:p | s:= s + ( p totalAmount).]. ^s.