© m.e. fayad 2000 -- 2006 sjsu -- cmpe software systems engineering dr. m.e. fayad, professor...

76
© M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering 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

Post on 20-Dec-2015

225 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU -- CmpE

Software Systems Engineering

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S2 Objects & Classes

2

Lesson 14:Objects & Classes

Page 3: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S3 Objects & Classes

Lesson Objectives

Objectives

3

Overview of Previous Lecture

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

Discuss the following:

– Different Kind of Classes

– Class Diagrams

Page 4: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S4 Objects & Classes

Notation

Notation Again !

Modeling Notation

4

Page 5: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S5 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S6 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S7 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S8 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S9 Objects & Classes

Classes (2)

9

RegistrationForm

RegistrationManager

Course

Student

CourseOfferingProfessor

ScheduleAlgorithm

Page 10: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S10 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S11 Objects & Classes

Attributes

11

Each course offeringhas a number, location and time

CourseOffering

numberlocationtime

Page 12: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S12 Objects & Classes

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: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S13 Objects & Classes

ResponsibilityResponsibility

A responsibility is a contract or an obligation of a classA responsibility is a contract or an obligation of a class

13

RegistrationManager

Responsibility:-- manage the registration

process

Page 14: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S14 Objects & Classes

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

14

Page 15: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S15 Objects & Classes

Example Classes

15

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 16: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S16 Objects & Classes

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?

16

Page 17: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S17 Objects & Classes

17

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 18: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S18 Objects & Classes

18

Classes in UML (2)

ParameterizedClass

ParameterizedClass<Parameter>

i:Element

normalOperation()abstractOperation()/derivedOperation()

classOperation()publicOperation()

#protectedOperation()-privateOperation()

Class

normalAttribute/derivedAttribute

classAttributepublicAttribute

#protectedAttribute-privateAttribute

Class

Page 19: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S19 Objects & Classes

Related Terms: Type

Definition: Class = Attributes + Operations

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

Notation:

19

Classes in UML (3)

Class

attribute1attribute2

operation1()operation2()

Class

attribute1attribute2

Class

attribute1attribute2

Class

Class

operation1()operation2()

operation1()operation2()

Class

Page 20: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S20 Objects & Classes

<< .. >> stereotype

{ .. } tagged values

:: separate package and class names

20

Classes in UML (4)

attribute: Type=InitialValue {Constraints}

operation(Paramter) {Constraints}

<<Stereotype>>Package::Class

{PropertyValues}

Page 21: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S21 Objects & Classes

21

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 22: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S22 Objects & Classes

In Smalltalk and CLOS, classes are simply objects, that is, classes can be sent messages, and they can include (class) attributes.

In C++, class attributes and operations can be emulated by declaring them as static.

However, in C++ classes cannot be treated like objects. An example for a class message or a class operation is new, which is used to create a new instance of a class.

In Smalltalk for example:

newObject := Class new;

anEmployee := Employee new;

Classes of class objects are called metaclasses and similarly to a normal class, noted with a stereotype <<metaclass>>

22

Classes in UML (6) - Metaclasses

<<metaclass>>CustomerClass

Page 23: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S23 Objects & Classes

In UML, metaclass is a class describing itself or other classes.

In UML, class operations need not be noted within the metaclass; they may also be contained in the class itself, where they are underlined in order to distinguish them from normal operations.

23

Classes in UML (7) - Metaclasses

numInstance: Integername: String

birthDate: Date

new(name)age(): Integer

Person

numInstance: Integer

new(name)

<<metaclass>>PersonClass

<<instance of>>

Page 24: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S24 Objects & Classes

Definition:

A parameterized class is a template equipped with generic formal parameters, which can be used to generate common (non-parameterized) classes.

The generic parameters serve as placeholders for the actual parameters which represent classes or simple data types.

Description:

In a parameterized class, no concrete class is defined, but only a template for generation of classes.

These templates are usually a type of macro technique which has no special function, apart from a text replacement.

In statically typed languages, parameterized classes are an important means for writing re-usable code.

C++ and Eiffel support parameterized classes. 24

Classes in UML (8) - Parameterized Classes

Page 25: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S25 Objects & Classes

A class generated with the aid of a parameterized class is called a bound element.

Notation

25

Classes in UML (9) - Parameterized Classes

i:Element

WaitingQueue

add()remove()

TrafficJam

WaitingRoom

<<bind>> (Car)

<<bind>> (Patient)

WaitingQueue<Car>

Page 26: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S26 Objects & Classes

Related Terms: virtual class

Definition:

– An Abstract class is never used to generate object instances.

– It is intentionally incomplete and thus forms the basis for further subclasses which can have instances.

Description:

Abstract classes often represent a general term, a generic term for a set of concrete terms.

Thus a vehicle can be an abstract generic term for bicycle, car, truck, train, and airplane.

Vehicle in this case is merely an abstraction, a generalization.26

Classes in UML (9) - Abstract Classes

Page 27: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S27 Objects & Classes

An abstract class is always a superclass.

An abstract class that has no subclasses makes no sense. Either it is superfluous, or it lacks a concrete class as subclass.

Notation

27

Classes in UML (9) - Abstract Classes

Class{abstract}

attributes

Operations()

Class

attributes

Operations()

Class

Class{abstract}

Page 28: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S28 Objects & Classes

Example

28

Classes in UML (9) - Abstract Classes

GeomFigure

Triangle Circle Rectangle

Abstract Class

Concrete Class

Page 29: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S29 Objects & Classes

Utility classes are collections of global variables and functions which are combined into a class.

The stereotype <<utility>> marks a class as a utility class

Utility classes are not true classes, but collection of global variables and functions which are, however, noted in the form of a class.

In OO, they are usually not needed.

29

Classes in UML (9) - Utility Classes

sin(angle): Realcos(angle): Realtan(angle): Realcot(angle): Real

<<utility>>MathFun

Page 30: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S30 Objects & Classes

30

Objects in UML (1)

Object : Class

attributeName = value

Object

:Class

ActiveObject

Multiobject

Page 31: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S31 Objects & Classes

31

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 32: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S32 Objects & Classes

32

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 33: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S33 Objects & Classes

33

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 34: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S34 Objects & Classes

34

Objects in UML (5)

Example:

aCircle: Circle

Radius = 25Center = (10,10)

Instance name

Attribute names

Class name

Attribute values

Page 35: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S35 Objects & Classes

35

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 36: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S36 Objects & Classes

36

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 37: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S37 Objects & Classes

37

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 38: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S38 Objects & Classes

38

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 39: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S39 Objects & Classes

39

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 40: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S40 Objects & Classes

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).

40

Operations, Methods in UML (2)

Page 41: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S41 Objects & Classes

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.

41

Operations, Methods in UML (3)

Page 42: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S42 Objects & Classes

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)}

42

Operations, Methods in UML (4)

Page 43: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S43 Objects & Classes

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!

43

Operations, Methods in UML (5)

Page 44: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S44 Objects & Classes

Related terms: usage context, constraint Definition:

– Stereotypes are project-, enterprise-, or method-specific extensions of pre-existing model elements of the UML metamodel. According to the semantics defined with the extension, the model element to which the stereotype is applied are semantically directly affected.

– In practice, stereotypes mainly specify possible usage contexts of a class, a relationship, or a package.

44

Stereotypes in UML (1)

Page 45: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S45 Objects & Classes

Description:– A stereotype is UML’s way of attaching extra

classifications to model items; it is one of the ways that UML is made extensible. The stereotype describes a model element, and is placed close to the affected element on a diagram, giving extra information about that element

– Some stereotypes are predefined in UML; they are automatically available and cannot be redefined. <<interface>>, <<type>>, and <<implementation class>> are examples. 45

Stereotypes in UML (2)

Page 46: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S46 Objects & Classes

Description: (continued)– Stereotypes can be defied to express

whatever extra classification may be deemed useful.

– For example, if an application had persistent classes, the stereotype <<persistent>> could be defined to show which classes are persistent.

46

Stereotypes in UML (3)

Page 47: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S47 Objects & Classes

Notation:– The stereotype is placed before or above the

element name and enclosed in French quotes (<<>>)

– Alternatively, special symbols may be used (decorative stereotypes). These can be seen in Rational Rose. Some of the elements that are represented in this manner are: <<actor>>, <<control>>, <<entity>>, and <<boundary>>. 47

Stereotypes in UML (4)

Page 48: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S48 Objects & Classes

Examples:– Stereotypes can, for example, be used to

indicate the meaning of a class in the application architecture, such as:

• <<presentation>>, <<process>>, <<domain class>>.

– Further examples:• <<model>>, <<view>>, <<controller>>,

<<exception>>, <<primitive>>, <<enumeration>>, <<signal>>, <<complete>>, <<incomplete>>, <<implements>>, <<uses>>, <<extends>>

48

Stereotypes in UML (4)

Page 49: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S49 Objects & Classes

Definition:– Interfaces describe a selected part of the

externally visible behavior of model elements (mostly of classes and components).

– Interface classes are abstract classes which define abstract operations, exclusively.

49

Interfaces & Interface Classes in UML (1)

Page 50: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S50 Objects & Classes

Description:– Interfaces are specifications of the external behavior of

classes and contain a set of signatures for operations that classes wishing to provide this interface need to implement.

– Operations in an interface need not be explicitly marked as {abstract}, because this is mandatory.

– Common classes that wish to implement an interface need to provide all the operations defined in the corresponding interface class.

50

Interfaces & Interface Classes in UML (2)

Page 51: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S51 Objects & Classes

Notation:– Interface classes are noted in the same way as

common classes, except that they bear the stereotype <<interface>>.

– They do not need a compartment for attributes, as they contain only operations.

– Operations in interface classes define only signatures; they are abstract and should therefore be set in italics. 51

Interfaces & Interface Classes in UML (3)

Page 52: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S52 Objects & Classes

52

Interfaces & Interface Classes in UML (4)

Example:

String

isEqual(String):BooleanisGreater(String):Boolean

Length():Integer

<<interface>>Sortable

isEqual(Object):BooleanisGreater(Object):Boolean

Realization Relationship

Page 53: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S53 Objects & Classes

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.

53

Constraints in UML (1)

Page 54: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S54 Objects & Classes

Description:– A constraint describes a condition or integrity

rule.

Notation:– Constraints are enclosed in braces:

• { Constraint }

54

Constraints in UML (2)

Page 55: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S55 Objects & Classes

Dependency

55

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 56: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S56 Objects & Classes

Consistency

56

Constraints’ Examples in UML (4)

Customer

Contract

1

0..*

has

receives

based on

Invoice self.contract.customer = self.customer

Invoice

1

1

0..*

Page 57: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S57 Objects & Classes

OR

57

Constraints’ Examples in UML (5)

Person

Domesticaddress

{or}

*

*

has

Foreignaddress

has

Page 58: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S58 Objects & Classes

Values

58

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 59: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S59 Objects & Classes

Order

59

Constraints’ Examples in UML (7)

NameList1..*contains

{ordered: lastName}

1 Person

lastNamefirstName

Page 60: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S60 Objects & Classes

Formulas

60

Constraints’ Examples in UML (8)

Person

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

{/age = today - birthDate}

Person

birthDate : Date/age

Page 61: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S61 Objects & Classes

Enumerations

61

Constraints’ Examples in UML (9)

ShoppingSpree1..*

paricipants

{participant.age > 65}1 Person

age

Page 62: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S62 Objects & Classes

Related terms: property string, feature, characteristic, constraint.

Definition:– Tagged values are user-defined, language and

tool specific keyword/value pairs which extend the semantics of individual model elements with specific characteristic properties.

62

Tagged Values in UML (1)

Page 63: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S63 Objects & Classes

Description:– Tagged values add specific additional properties

to existing model elements. They detail the semantics of a model element and can influence code generation.

Notation:– Tagged values consist of a keyword and a

value, and are enclosed in braces.

63

Tagged Values in UML (2)

Page 64: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S64 Objects & Classes

64

Tagged Values in UML (3)

Examples:– {abstract}– {readonly}– {private}– {obsolete}– {version=2.1}

GeomFigure{abstract Version=1.3}

visible : Boolean {readonly}

display() {abstract}remove() {abstract}getPosition(): PointsetPosition(p: Point)

setPos(x,y) {obsolete}

Page 65: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S65 Objects & Classes

Related terms: category, subsystem Definition:

– Packages are collections of model elements of arbitrary types which are used to structure the entire model into smaller clearly visible units.

– A package defines a namespace, that is , the names of the elements within a package must be unique.

– Each model element can be referenced in other packages, but it belongs to exactly a single (home) package.

– The package can in turn contain packages. The top package includes the entire system.

65

Packages in UML (1)

Page 66: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S66 Objects & Classes

Description:– Packages my contain different model elements,

for example, classes and use cases. They may be hierarchically structured, with packages inside packages.

– Packages are built on the basis of logical or physical relationships.

66

Packages in UML (2)

Page 67: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S67 Objects & Classes

Description:– A model element can be contained in several

packages, but each element has its home package. In all other packages, it can only be quoted in the form:

• PackageName::ClassName

– This creates dependencies between the packages: one package uses classes of another package. 67

Packages in UML (3)

Page 68: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S68 Objects & Classes

Description:– A good architecture leads to few

dependencies between packages. – Packages are also suitable working entities

for project management and organization.

68

Packages in UML (4)

Page 69: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S69 Objects & Classes

Notation – A package is represented as a folder. Inside this symbol,

the name of the package is noted. – The dependencies between packages are noted by a

dashed arrow which points toward the independent package.

– It is also possible to represent generalization relations between packages if the model elements contained in the packages are generalizations or specializations of elements contained in other packages.

69

Packages in UML (5)

Page 70: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S70 Objects & Classes

70

Packages in UML (6)

ExampleProductProduct

Online Tariffing

Online Tariffing

Tariffing Life

Tariffing Life

LifeLife

Contents&PL

Contents&PL

PartnerPartnerTariffing Contents

Tariffing Contents

Page 71: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S71 Objects & Classes

71

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 72: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S72 Objects & Classes

72

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.

Page 73: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S73 Objects & Classes

Related items: mechanism

Description: Class models and the like are substantially easier to understand when one knows where design patterns have been employed.

Therefore, it is sensible to document this in the model in an appropriate manner. 73

Cooperation / Design Pattern Notation in UML (1)

Page 74: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S74 Objects & Classes

74

Cooperation / Design Pattern Notation in UML (2)

Productelement

ProductProduct

price

Contract11..*

composite

Composite(Gamma et al. 1995)

sheet

componentclient

Page 75: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S75 Objects & Classes

• T/F statements

1. Abstract classes are used to generate object instances

2. Legacy Software is any preexisting software that must be replaced by, incorporated into, or interfaced with software that is currently being developed

3. Utility class contains global variables and functions.

4. Design patterns identify, name, and describe common and recurring designs appearing frequently in object-oriented systems.

• Define in UML: Abstract Classes, Objects, Metaclasses, Parameterized Classes, Utility Classes, and Notes

75

Discussion Questions

Page 76: © M.E. Fayad 2000 -- 2006 SJSU -- CmpE Software Systems Engineering Dr. M.E. Fayad, Professor Computer Engineering Department, Room #283I College of Engineering

© M.E. Fayad 2000 -- 2006 SJSU – CmpE M.E. Fayad L3-5a-S76 Objects & Classes

76

Questions for the Next Lecture

Define:–RRelationships–AAssociation relationships

Aggregation

Inheritance

–A