1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

27
1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling http://www.uic.edu.hk/~kentsang/SWE/SWE.htm

Transcript of 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Page 1: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

1

Software EngineeringDr. K. T. Tsang

Lecture 5

Class modelinghttp://www.uic.edu.hk/~kentsang/SWE/SWE.htm

Page 2: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Class & objects: focus of class modeling

Person

Class

Objects

JoeSmith:Person

MarySmith:Person

:Person

Anonymous object

Page 3: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Attributes & values

Person

name: stringbirthday: datephoneNumber: string

JoeSmith:Person

name=“JoeSmith”birthday=21 October 1988phoneNumber=555666

name=“MarySmith”birthday=12 May 1977phoneNumber=222333

MarySmith:Person

Attribute nametype

value

Page 4: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Operations and methods

• An operation is a function/procedure that may be applied to or by objects in the class.

• A method is the implementation of an operation for a class.

• When an operation has methods on several classes, it is important that the methods all have the same signature – the number & types of arguments & the type of return value.

Page 5: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Person

name: stringbirthday: datephoneNumber: string

method name

Operations and methods (2)

changeJobchangeAddress

File

fileNamesizeInByteslastUpdate

colorposition

GeometricObject

print

move(delta: Vector)select(p: Point): Booleanrotate(in angle: float=0.0)

directiontype

Default value

Argument name

Return type

Page 6: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Direction of arguments

• The direction of an argument indicates whether it is an input (in), output (out), or an input that can be modified (inout).

Page 7: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Link & Association

• A link is a physical or conceptual connection among objects.

• An association is – a description of a group of links with common

structure and common semantics.– a set of potential links in the same way that a

class describes a set of potential objects.

Page 8: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Multiplicityis the number of instances of one class that may

relate to a single instance of an associated class.

• UML diagrams explicitly list multiplicity at the ends of the association lines:– “1” exactly one– “1..*” one or more– “3..5” three to five, inclusive– “*” many (zero or more)

• Cardinality is the actual number of instances that is related to a single instance of an associated class.

• Multiplicity is a constraint on the cardinality.

Page 9: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Link & Association: example

Person

name: stringbirthday: datephoneNumber: string

Company

name

OwnsStock* *many to many

Joe:Person

name=“Joe”

Mary:Person

name=“Mary”

Amy:Person

name=“Amy”

Microsoft:Company

name=“Microsoft”

IBM:Company

name=“IBM”

Page 10: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Association End Names: example

Joe:Person

name=“Joe”

Mary:Person

name=“Mary”

Amy:Person

name=“Amy”

Microsoft:Company

name=“Microsoft”

IBM:Company

name=“IBM”

Person

name: stringbirthday: datephoneNumber: string

Company

name

WorksFor* 0..1employee employer

Page 11: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Association End Names: example

Person

name: stringbirthday: datephoneNumber: string

parent

child *

0..2

Page 12: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Ordering of association

Screen Window1 *

{ordered}

VisibleOn

Page 13: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Bags & Sequences• Bag – a collection of elements with duplicates

allowed.

• Sequence – an ordered collection of elements with duplicates allowed.

Itinerary Airport* *

{sequence}

Page 14: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Association classes

• It is an association that is also a class

• It has attributes.

File User

* *AccessibleBy

accessPermission

/etc/temp read John

/etc/temp read-write Mary

/usr/bin read-write John

Page 15: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Association classes - example

Person

name: stringbirthday: datephoneNumber: string

Company

name

* 0..1employee employer

Workfor

salaryjobTitle

Manages

PerformanceRating

*

0..1

worker

boss

Page 16: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Association class or ordinary class

Person

name: stringbirthday: datephoneNumber: string

Company

name

* *

OwnStock

quantity

Person

name: stringbirthday: datephoneNumber: string

Company

name* *Purchase

quantitydatacost

1 1

Page 17: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Qualified association

Bank Account

accountNumberBank Account 1 0..1

1 *

Page 18: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Qualified association

StockExchange

Company

tickerSymbol

StockExchange

Company

Lists

tickerSymbol

*

*

*0..1

Lists

qualifiedunqualified

Page 19: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Generalization & Inheritance

• Generalization – the relationship between a class (superclass) and its specialization (subclasses)

• Also called “is-a” (is-kind-of) relationship

• 3 purposes– Organize a hierarchy– Enable code reuse– Support polymorphism – overriding features of

the superclass

Page 20: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Fig.3.25 in p.39 B & R

Inheritance for graphic figures

Page 21: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Fig.3.28 in p.46 B & R

Page 22: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Fig.3.27 in p.44 B & R

Page 23: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Object constraint language - OCL

• Attributes – source_object + “.” + attribute_name

• Operations - source_object + “.” + operation_name + “()”

• Simple association – traverse an association : aCreditCardAccount.MailingAddress

• Qualified association – more precise traversal : aCreditCardAccount.Statement[10 Aug 2007]

• Filters – most commonly used : “select”– aStatement.Transaction->select(amount>$100)

Page 24: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Class Responsibility Collaborator cards

• CRC cards, being proposed by Ward Cunningham and Kent Beck, are a brainstorming tool used in the design of object-oriented software.

• They are typically used when first determining which classes are needed and how they will interact.

• http://anonymouse.org/cgi-bin/anon-www.cgi/http://en.wikipedia.org/wiki/Crc_cards

Page 25: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

CRC cards..1

CRC cards are usually created from index cards on which are written:

• The class name

• Its Super and Sub classes (if applicable)

• The responsibilities of the class.

• The names of other classes with which the class will collaborate to fulfill its responsibilities.

• Author

Page 26: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

• Using a small card keeps the complexity of the design at a minimum. It focuses the designer on the essentials of the class and prevents him from getting into its details and inner workings at a time when such detail is probably counter-productive.

CRC cards..2

Page 27: 1 Software Engineering Dr. K. T. Tsang Lecture 5 Class modeling kentsang/SWE/SWE.htm.

Reading for this lecture

• Chapter 3 Blaha & Rumbaugh

• Study the class models in examples– http://www.cs.gordon.edu/courses/cs211/

AddressBookExample/index.html– http://www.cs.gordon.edu/courses/cs211/

ATMExample/index.html