110
Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

Embed Size (px)

Citation preview

Page 1: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

Mastering Object-Oriented Analysis and Design with UMLModule 13: Class Design

Mastering Object-Oriented Analysis and Design with UMLModule 13: Class Design

Page 2: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi2

Objectives: Class Design

Define the purpose of Class Design and where in the lifecycle it is performed

Identify additional classes and relationships needed to support implementation of the chosen architectural mechanisms

Identify and analyze state transitions in objects of state-controlled classes

Refine relationships, operations, and attributes

Page 3: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi3

Class Design in Context[Early

Elaboration Iteration]

[Inception Iteration (Optional)]

Define a CandidateArchitecture

PerformArchitectural

Synthesis

Analyze Behavior

Refine theArchitecture

DefineComponents

Design theDatabase

(Optional)

ClassDesign Designer

Page 4: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi4

Class Design Overview

SupplementarySpecifications

ClassDesign

Project SpecificGuidelines

Design Classes

Page 5: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi5

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Nonfunctional Requirements in General Checkpoints

Page 6: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi6

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 7: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi7

Class Design Considerations Class stereotype

Boundary Entity Control

Applicable design patterns Architectural mechanisms

Persistence Distribution etc.

Page 8: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi8

A class should have a single well-focused purpose. A class should do one thing and do it well!

How Many Classes Are Needed?

Many, simple classes means that each class Encapsulates less of the overall system

intelligence Is more reusable Is easier to implement

A few, complex classes means that each class Encapsulates a large portion of the overall

system intelligence Is less likely to be reusable Is more difficult to implement

Page 9: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi9

Strategies for Designing Boundary Classes

User interface (UI) boundary classes What user interface development tools will be used? How much of the interface can be created by the

development tool? External system interface boundary classes

Usually model as subsystem

MainForm

SubWindow

DropDownListButton

MainWindow

Page 10: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi10

Strategies for Designing Entity Classes

Entity objects are often passive and persistent Performance requirements may force some re-factoring See the Identify Persistent Classes step

Analysis

FatClassLazyDataHelper

- rarelyUsedAttr1- rarelyUsedAttr2

Design

FatClass

- privateAttr

+ getCommonlyUsedAttr1()+ getCommonlyUsedAttr2()+ getRarelyUsedAtt1()+ getRarelyUsedAtt2()

FatClassDataHelper

- commonlyUsedAttr1- commonlyUsedAttr2

1 0..1

FatClass

- privateAttr- commonlyUsedAttr1- commonlyUsedAttr2- rarelyUsed1- rarelyUsed2

<< Entity >>

Page 11: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi11

Strategies for Designing Control Classes

What happens to Control Classes? Are they really needed? Should they be split?

How do you decide? Complexity Change probability Distribution and performance Transaction management

Page 12: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi12

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 13: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi13

Messages displayed in interaction diagrams

Other implementation dependent functionality Manager functions Need for class copies Need to test for equality

Operations: Where Do You Find Them?

: ClassA

1 : V/Perform Responsibility\

: ClassB : ClassA

1 : \performResponsibility (): result\

: ClassB

Page 14: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi14

Name and Describe the Operations

Create appropriate operation names Indicate the outcome Use client perspective Are consistent across classes

Define operation signatures operationName([direction]parameter : class,..) :

returnType• Direction is in, out or inout with the default in

if absent Provide short description, including

meaning of all parameters

Page 15: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi15

Guidelines: Designing Operation Signatures

When designing operation signatures, consider if parameters are: Passed by value or by reference Changed by the operation Optional Set to default values In valid parameter ranges

The fewer the parameters, the better Pass objects instead of “data bits”

Page 16: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi16

Public operations

Protected operations

Privateoperations

Operation Visibility

Visibility is used to enforce encapsulation May be public, protected, or private

Page 17: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi17

How Is Visibility Noted?

The following symbols are used to specify export control: + Public access # Protected access - Private access

Class1

- privateAttribute+ publicAttribute# protectedAttribute

- privateOperation ()+ publicOPeration ()# protecteOperation ()

Page 18: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi18

Scope

Determines number of instances of the attribute/operation Instance: one instance for each class instance Classifier: one instance for all class instances

Classifier scope is denoted by underlining the attribute/operation name

Class1

- classifierScopeAttr- instanceScopeAttr

+ classifierScopeOp ()+ instanceScopeOp ()

Page 19: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi19

Example: Scope

Student

- name- address

- nextAvailID : int

+ addSchedule ([in] theSchedule : Schedule, [in] forSemester : Semester)+ getSchedule ([in] forSemester : Semester) : Schedule+ hasPrerequisites ([in] forCourseOffering : CourseOffering) : boolean# passed ([in] theCourseOffering : CourseOffering) : boolean+ getNextAvailID () : int

<<Entity>>

- studentID

Page 20: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi20

Example: Define Operations

CourseOffering<<Entity>>

Student

+ getTuition() : double+ addSchedule ( [in] aSchedule : Schedule)+ getSchedule ( [in] forSemester : Semester) : Schedule+ deleteSchedule ( [in] forSemester : Semester)+ hasPrerequisites ( [in] forCourseOffering : CourseOffering) : boolean# hasPassed ( [in] aCourseOffering : CourseOffering) : boolean+ getNextAvailID() : int+ getStudentID() : int+ getName() : String+ getAddress() : String

<<Entity>>

RegistrationController

+ submitSchedule()+ saveSchedule()+ getCourseOfferings() : CourseOfferingList+ getCurrentSchedule ( [in] forStudent : Student, [in] forSemester : Semester) : Schedule+ deleteCurrentSchedule()+ new ( [in] forStudentID : String)+ getStudent ( [in] anID : int) : Student

<<control>>

Schedule<<Entity>>

0..1

0..1+ registrant 0..*

1

0..1

0..1

+currentSchedule

0..*0..*

+primaryCourses

0..4

+alternateCourses

0..2

ICourseCatalogSystem

+ getCourseOfferings()+ initialize()

<<Interface>>

1

0..*

Page 21: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi21

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 22: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi22

Define Methods

What is a method? Describes operation implementation

Purpose Define special aspects of operation

implementation Things to consider:

Special algorithms Other objects and operations to be used How attributes and parameters are to be

implemented and used How relationships are to be implemented and

used

Page 23: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi23

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 24: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi24

Define States

Purpose Design how an object’s state affects its

behavior Develop statecharts to model this behavior

Things to consider : Which objects have significant state? How to determine an object’s possible states? How do statecharts map to the rest of the

model?

Page 25: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi25

What is a Statechart?

A directed graph of states (nodes) connected by transitions (directed arcs)

Describes the life history of a reactive object

State State

Transition

Guard Condition

ActionEventState1 State2

Event(args)[guard condition]/action

Page 26: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi26

Special States

Initial state The state entered when an

object is created Mandatory Can only have one initial

state

Final state Indicates the object’s end

of life Optional May have more than one

State1

State2

Page 27: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi27

Significant, dynamic attributes

Existence and non-existence of certain links

Identify and Define the States

The maximum number of students per course offering is 10

numStudents < 10 numStudents < = 10

Link to ProfessorExists

Link to ProfessorDoesn’t Exist

Professor<< Entity >>

CourseOffering<< Entity >>

Open Closed

0..*

0..1 Assigned Unassigned

Page 28: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi28

Look at the class interface operations

Events: addProfessor, removeProfessor

Identify the Events

CourseOffering<< Entity >>

+ addProfessor()+ removeProfessor()

0..*

0..1Professor<< Entity >>

Page 29: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi29

Identify the Transitions

For each state, determine what events cause transitions to what states, including guard conditions, when needed

Transitions describe what happens in response to the receipt of an event

CourseOffering<< Entity >>

+ addProfessor()+ removeProfessor()

0..*

0..1Professor<< Entity >>

Assigned

Unassigned

removeProfessor addProfessor

Page 30: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi30

StateC

Exit/someAction

StateB

Do/anActivity

StateA

Add Activities and Actions

Activities Are associated with a state Start when the state is

entered Take time to complete Interruptible

Actions Associated with a transition Take an insignificant amount

of time to complete Are non-interruptible

Action

Activity

Entry/anAction

Event[condition]/action

Page 31: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi31

Example: StatechartaddStudent / numStudents = numStudents + 1

Unassigned

Assigned

Full

Canceled

do: Send cancellation notices

Committed

do: Generate class roster

closeRegistration [ has Professor assigned ]

close

/ numStudents = 0

addProfessor

closeRegistration

removeStudent [numStudents >0]/ numStudents = numStudents - 1

cancel

removeProfessor

[ numStudents = 10 ]

close[ numStudents < 3 ]

closeRegistration[ numStudents >= 3 ]

close[ numStudents >= 3 ]

addStudent /numStudents = numStudents + 1

cancel

removeStudent[ numStudents > 0] / numStudents = numStudents - 1

close

[ numStudents = 10 ]cancel

Page 32: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi32

Example: Statechart with Nested States and Historysuperstate

substate

addStudent /numStudents = numStudents + 1

Open

Unassigned

Assigned

H

add a professor

Closed

Canceled

do: Send cancellation notices

Full

Committed

do: Generate class roster

closeRegistration

close

remove a professor

close[ numStudents < 3 ]

[ numStudents = 10 ]

closeRegistration[ numStudents >= 3 ]

close[ numStudents >= 3 ]

closeRegistration [ has Professor assigned ]

close

/ numStudents = 0

removeStudent[ numStudents > 0] / numStudents = numStudents - 1

cancelcancel

Page 33: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi33

Which Objects Have Significant State?

Objects whose role is clarified by state transitions

Complex use cases that are state-controlled It is not necessary to model objects such

as: Objects with straightforward mapping to

implementation Objects that are not state-controlled Objects with only one computational state

Page 34: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi34

Events may map to operations Methods should be updated with state-specific

information States are often represented using attributes

This serves as input into the “Define Attributes” step

(Stay tuned for derived attributes)

How Do Statecharts Map to the Rest of the Model?

Open

CourseOffering<< Entity >>

- numStudents

+ addStudent()

Full[numStudents=10]

addStudent / numStudents = numStudents + 1

Page 35: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi35

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 36: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi36

Attributes: How Do You Find Them?

Examine method descriptions Examine states Examine any information the class itself

needs to maintain

Page 37: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi37

Attribute Representations

Specify name, type, and optional default value attributeName : Type = Default

Follow naming conventions of implementation language and project

Type should be an elementary data type in implementation language Built-in data type, user-defined data type, or

user-defined class Specify visibility

Public: + Private: - Protected: #

Page 38: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi38

Derived Attributes

What is a derived attribute? An attribute whose value may be calculated

based on the value of other attribute(s) When do you use it?

When there is not enough time to re-calculate the value every time it is needed

When you must trade-off runtime performance versus memory required

Page 39: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi39

Example: Define Attributes

RegistrationController<< Control >>

0..1

Student<< Entity >>

- name- address- nextAvailID : int- studentID : int- dateOfBirth : Date

ICourseCatalogSystem<< interface >>

Schedule<< Entity >>

CourseOffering<< Entity >>

- number : String = “100”- startTime : Time- endTime : Time- day : String- /numStudents : int = ()

- semester : Semester

0..1

0..1

0..1

+ registrant + currentSchedule

0..*

0..40..2

0..*

+ alternateCourses

10..*

+ primaryCourses

Page 40: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi40

(continued)

Exercise 1: Class Design

Given the following: The architectural layers, their packages,

and their dependencies Design classes for a particular use case

Page 41: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi41

Exercise 1: Class Design (cont.)

Identify the following: Attributes, operations, and their

complete attribute signatures Attribute and operation scope

and visibility Any additional relationships

and/or classes to support the defined attributes and attribute signatures

Class(es) with significant state-controlled behavior

The important states and transitions for the identified class

(continued)

Page 42: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi42

Produce the following: Design Use-Case Realization

• Statechart for one of the classes that exhibits state-controlled behavior

• Class diagram (VOPC) that includes all operations, operation signatures, attributes, and attribute signatures

Exercise 1: Class Design

Page 43: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi43

Exercise 1: Review

Compare your results Is the name of each operation

descriptive and understandable? Does the name of the operation indicate its outcome?

Does each attribute represent a single conceptual thing? Is the name of each attribute descriptive and does it correctly convey the information it stores?

Is the state machine understandable? Do state names and transitions reflect the context of the domain of the system? Does the state machine contain any superfluous states or transitions?

Payroll System

Page 44: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi44

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 45: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi45

What Is a Dependency? A relationship between two objects

Purpose Determine where structural relationships are

NOT required Things to look for :

What causes the supplier to be visible to the client

Define Dependency

SupplierClient

Page 46: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi46

Dependencies vs. Associations

Associations are structural relationships

Dependencies are non-structural relationships

In order for objects to “know each other” they must be visible Local variable reference Parameter reference Global reference Field reference Association

Dependency

Supplier2

Client

Supplier1

Page 47: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi47

Associations vs. Dependencies in Collaborations

An instance of an association is a link All links become associations unless they have

global, local, or parameter visibility Relationships are context-dependent

Dependencies are transient links with: A limited duration A context-independent relationship A summary relationship

A dependency is a secondary type of relationship in that it doesn't tellyou much about the relationship. For details you need to consult thecollaborations.

Page 48: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi48

Local Variable Visibility

The op1() operation contains a local variable of type ClassB

Class Diagram Collaboration Diagram

ClassA :ClassA

+ op1 ( )

ClassB :ClassBL

Page 49: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi49

Parameter Visibility

The ClassB instance is passed to the ClassA instance

Class Diagram Collaboration Diagram

ClassA :ClassA

+ op1 ( [in] aParam : ClassB )

ClassB :ClassBP

Page 50: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi50

Global Visibility

The ClassUtility instance is visible because it is global

Class Diagram Collaboration Diagram

ClassA :ClassA

+ op1 ( )

ClassB :ClassBG

+ utilityOp ( )

Page 51: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi51

Permanent relationships — Association (field visibility) Transient relationships — Dependency

Multiple objects share the same instance

• Pass instance as a parameter (parameter visibility)• Make instance a managed global (global visibility)

Multiple objects don’t share the same instance (local visibility)

How long does it take to create/destroy? Expensive? Use field, parameter, or global visibility Strive for the lightest relationships possible

Identifying Dependencies: Considerations

Page 52: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi52

Example: Define Dependencies (before)ICourseCatalogSystem

+ getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingList

<<interface>>

Student

- name- address- StudentID : int

+ addSchedule ( [in] aSchedule : Schedule )+ getSchedule ( [in] forSemester : Semester ) : Schedule+ hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean# passed ( [in] aCourseOffering : CourseOffering ) : boolean

<<Entity>>

RegistrationController

+ // submit schedule ()+ // save schedule ()+ // create schedule with offerings ()+ // get course offerings ()

<<Control>>

0..1

0..1

+ registrant

0..* 1

+ courseCatalog

Schedule

- semester : Semester

+ submit ()+ //save ()# any conflicts? ()+ //create with offerings()

<<entity>>

0..*

1

0..1

0..1

+ currentSchedule

CourseOffering

- number : String = "100"- startTime : Time- endTime : Time- day : String

+ addStudent ( [in] aStudentSchedule : Schedule)+ removeStudent ( [in] aStudentSchedule : Schedule)+ new ()+ setData ()

<<Entity>>

0..*

0..4

+ primaryCourses

0..*

0..2

alternateCourses

Page 53: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi53

Example: Define Dependencies (after)ICourseCatalogSystem

+ getCourseOfferings ( [in] forSemester : Semester) : CourseOfferingList

<<interface>>RegistrationController

+ // submit schedule ()+ // save schedule ()+ // create schedule with offerings ()+ // get course offerings ()

<<Control>>

0..1

+ registrant

Schedule

- semester : Semester

+ submit ()+ //save ()# any conflicts? ()+ //create with offerings()

<<entity>>

0..*

0..1

0..1

+ currentSchedule

CourseOffering

- number : String = "100"- startTime : Time- endTime : Time- day : String

+ addStudent ( [in] aStudentSchedule : Schedule)+ removeStudent ( [in] aStudentSchedule : Schedule)+ new ()+ setData ()

<<Entity>>

0..*

0..4

+ primaryCourses

0..*

0..2

alternateCourses

Global visibility

Parameter visibility

Field visibility Field visibility

Student

- name- address- StudentID : int

+ addSchedule ( [in] aSchedule : Schedule )+ getSchedule ( [in] forSemester : Semester ) : Schedule+ hasPrerequisites ( [in] forCourseOffering : CourseOffering ) : boolean# passed ( [in] aCourseOffering : CourseOffering ) : boolean

<<Entity>>

0..1 1

Page 54: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi54

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 55: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi55

Define Associations

Purpose Refine remaining associations

Things to look for : Association vs. Aggregation Aggregation vs. Composition Attribute vs. Association Navigability Association class design Multiplicity design

Page 56: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi56

What Is Composition?

A form of aggregation with strong ownership and coincident lifetimes The parts cannot survive the whole/aggregate

Whole

Composition

Part

PartWhole

Page 57: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi57

Shared Aggregation

Non-shared Aggregation

Aggregation: Shared vs. Non-shared

By definition, composition is non-shared aggregation

Whole Part1..* 0..*

Multiplicity > 1

Multiplicity = 1 Multiplicity = 1

1Whole Part1 0..* Whole Part0..*

Composition

Page 58: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi58

Aggregation or Composition?

Consideration Lifetimes of Class1 and Class2

aggregation

composition

Class1 Class2

Class1 Class2

Page 59: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi59

Example: Composition

1

0..*

ScheduleStudent

RegisterForCoursesForm 1

1

RegistrationController

Page 60: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi60

Attributes Vs Composition

Use composition when Properties need independent identities Multiple classes have the same properties Properties have a complex structure and

properties of their own Properties have complex behavior of their own Properties have relationships of their own

Otherwise use attributes

Page 61: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi61

Example: Attributes vs. Composition

Composition of separate class

Attribute

Student- name- address- nextAvailID : int- StudentID : int- dateofBirth : Date

+ addSchedule ()+ getSchedule ()+ delete Schedule ()+ hasPrerequisites ()# hasPassed ()

<<entity>>

Schedule

+ submit ()

+ //save ()

# any conflicts? ()

+ //create with offerings ()

+ new ()

+ passed ()

<<entity>>

- semester : Semester

0..*

1

Page 62: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi62

Review: What Is Navigability?

Indicates that it is possible to navigate from a associating class to the target class using the association

RegistrationController<<Control>>

CourseOffering<<Entity>>

Schedule<<Entity>>

Page 63: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi63

Navigability: Which Directions Are Really Needed? Explore interaction diagrams Even when both directions seem required, one

may work Navigability in one direction is infrequent Number of instances of one class is small

?0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

Page 64: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi64

Example: Navigability Refinement

Total number of Schedules is small, or

Never need a list of the Schedules on which the CourseOffering appears

Total number of CourseOfferings is small, or

Never need a list of CourseOfferings on a Schedule

Total number of CourseOfferings and Schedules are not small

Must be able to navigate in both directions

0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

0..40..*

+ primaryCourses CourseOfferingSchedule<<Entity>> <<Entity>>

Page 65: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi65

Association Class

A class is “attached” to an association

Contains properties of a relationship

Has one instance per link

ScheduleOfferingInfo

- status

+ // is selected ()+ // mark as selected ()+ // mark as cancelled ()

<<Entity>>

CourseOffering<<Entity>>

Schedule<<Entity>>

0..*0..4

+ primaryCourses

+ alternateCourses

0..* 0..2

PrimaryScheduleOfferingInfob

- grade

+ // is enrolled in? ()+ // mark as enrolled in ()+ // mark as committed ()

<<Entity>>

Page 66: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi66

Example: Association Class Design

Design Decisions

CourseOffering<<Entity>>

Schedule<<Entity>>

0..*0..4

+ primaryCourses

+ alternateCourses

0..* 0..2

PrimaryScheduleOfferingInfob- grade

+ // is enrolled in? ()+ // mark as enrolled in ()+ // mark as committed ()

<<Entity>>

CourseOffering<<Entity>>

Schedule<<Entity>>

1

0..*- theCourseOffering

+ alternateCourses0..* 0..2

PrimaryScheduleOfferingInfob- grade

+ // is enrolled in? ()+ // mark as enrolled in ()+ // mark as committed ()

<<Entity>>- primaryCourseOfferingInfo

0..4

1

Page 67: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi67

Multiplicity = 1, or Multiplicity = 0..1 May be implemented directly as a simple

value or pointer No further “design” is required

Multiplicity > 1 Cannot use a simple value or pointer Further “design” may be required

Multiplicity Design

Needs a container for

CourseOfferings

0..1 0..*<<Entity>> <<Entity>>

Professor CourseOffering+ Instructor

0..1 0..*<<Entity>> <<Entity>>

Professor CourseOffering+ Instructor

Page 68: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi68

Multiplicity Design Options

Explicit Modeling of a Container Class Detail Container via Note

0..1 0..*<<Entity>> <<Entity>>

Professor CourseOffering+ Instructor

0..1 0..*<<Entity>>Professor CourseOfferingList

+ Instructor

<<Entity>>CourseOffering 0..1 0..*

<<Entity>>Professor

+ Instructor

<<Entity>>CourseOffering

List

Page 69: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi69

What is a Parameterized Class (template)?

A class definition that defines other classes Often used for container classes

Some common container classes:

• Sets, lists, dictionaries, stacks, queues

ParameterizedClass

Formal Arguments

List

Item

Page 70: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi70

Instantiating a Parameterized Class

ParameterizedClass

Formal Arguments

InstantiatedClass ActualArgument

<<bind>> (ActualArgument)

Page 71: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi71

Example: Instantiating a Parameterized Class

Before

AfterList

Item

CourseOfferingList ActualArgument

<<bind>> (CourseOffering)

<<Entity>>1

0..*

CourseOfferingList CourseOffering<<Entity>>1

0..*

Page 72: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi72

Multiplicity Design: Optionality

If a link is optional, make sure to include an operation to test for the existence of the link

Professor CourseOffering

+ isTeaching () : boolean

0..1

0..*+ hasProfessor () : boolean

Page 73: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi73

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Class Design Steps

LightLight

Page 74: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi74

Review: Generalization

One class shares the structure and/or behavior of one or more classes

“Is a kind of” relationship

In Analysis, use sparingly

Superclass (Parent)

(Ancestor)

Generalization Relationship

Subclasses (Child)

(Descendants)

Account

+ balance+ name+ number

+ withdraw ()+ createStatement ()

Checking Savings

+ getInterest ()

Page 75: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi75

There are no direct instances of Animal

Lion Tiger

Animal

+ communicate ()

+ communicate ()+ communicate ()

Abstract and Concrete Classes

Abstract classes cannot have any objects Concrete classes can have objects

All objects are either lions or tigers

Abstract class

Abstract operation

Communication

Discriminator

Page 76: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi76

Name clashes on attributes or operations Repeated inheritance

Multiple Inheritance: Problems

Resolution of these problems is implementation-dependent

AnimateObject

Bird

Animal

+ color

+ getColor ()

FlyingThing

+ color

+ getColor ()

Bird

Animal

+ color

+ getColor ()

FlyingThing

+ color

+ getColor ()

Page 77: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi77

Generalization Constraints

Complete End of the inheritance tree

Incomplete Inheritance tree may be extended

Disjoint Subclasses mutually exclusive Doesn’t support multiple inheritance

Overlapping Subclasses are not mutually exclusive Supports multiple inheritance

Page 78: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi78

Example: Generalization Constraints

Asset

Real EstateBank Account Security

Savings CheckingStock Bond

{disjoint}

{disjoint,complete} {disjoint}

End of inheritance hierarchy

MultipleInheritance

not supported

Page 79: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi79

Example: Generalization Constraints (cont.)

Vehicle

AmphibiousVehicle

WaterVehicleLandVehicle

{overlapping}

Multipleinheritancesupported

Page 80: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi80

Is this correct?

Generalization vs. Aggregation

Generalization and aggregation are often confused Generalization represents an “is a” or “kind-of”

relationship Aggregation represents a “part-of” relationship

Window

WindowWithScrollbar

Scrollbar

Page 81: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi81

Window

WindowWithScrollbar

Scrollbar

Generalization vs. Aggregation

Scrollbar

Window

WindowWithScrollbar

1

1

A WindowWithScrollbar “is a” WindowA WindowWithScrollbar “contains a” Scrollbar

Page 82: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi82

Do these classes follow the “is a” style of programming?

Generalization: Share Common Properties and Behavior

Follows the “is a” style of programming Class substitutability

List

+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)

Stack

Lion Tiger

Animal

+ communicate ()

+ communicate ()+ communicate ()

Page 83: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi83

Generalization: Share Common Properties and Behavior (cont.)

List

+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)

Stack

Lion Tiger

Animal

+ communicate ()

+ communicate ()+ communicate ()

Page 84: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi84

Generalization: Share Implementation: Factoring

Supports the reuse of the implementation of another class

Cannot be used if the class you want to “reuse” cannot be changed

List

+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)

Stack

SequentialContainer

List

+ insertTop ([in] item)+ removeTop ()

Stack

+ insertBottom ([in] item)+ removeBottom ()+ insert ([in] item, [in] position)

Page 85: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi85

Generalization Alternative: Share Implementation: Delegation

Supports the reuse of the implementation of another class

Can be used if the class you want to “reuse” cannot be changed

List

+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)

Stack List

Stack

+ insertBottom ([in] item)+ removeBottom ()+ insert ([in] item, [in] position)

1

1

Page 86: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi86

push() and pop() can access methods of List but instances of Stack cannot

Implementation Inheritance

Ancestor public operations, attributes, and relationships are NOT visible to clients of descendent class instances

Descendent class must define all access to ancestor operations, attributes, and relationships

List

+ insertTop ([in] item)+ insertBottom ([in] item)+ removeTop ()+ removeBottom ()+ insert ([in] item, [in] position)

Stack

<<implementation>>

Page 87: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi87

Manufacturer AManufacturer B

Manufacturer C

OO Principle:Encapsulation

Review: What Is Polymorphism?

The ability to hide many different implementations behind a single interface

Page 88: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi88

Generalization: Implement Polymorphism

Without Polymorphism

if animal = “Lion” thenLion communicate

else if animal = “Tiger” thenTiger communicate

end

With Polymorphism

Animal communicate

Lion Tiger

Animal

+ communicate ()

+ communicate ()+ communicate ()

Page 89: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi89

Polymorphism: Use of Interfaces vs. Generalization

Interfaces support implementation-independent representation of polymorphism Realization relationships can cross generalization

hierarchies Interfaces are pure specifications, no behavior

Abstract base class may define attributes and associations

Interfaces are totally independent of inheritance Generalization is used to re-use implementations Interfaces are used to re-use behavioral specifications

Generalization provides a way to implement polymorphism

Page 90: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi90

Polymorphism via Generalization Design Decisions

Provide interface only to descendant classes? Design ancestor as an abstract class All methods are provided by descendent classes

Provide interface and default behavior to descendent classes? Design ancestor as a concrete class with a default

method Allow polymorphic operations

Provide interface and mandatory behavior to descendent classes? Design ancestor as a concrete class Do not allow polymorphic operations

Page 91: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi91

Metamorphosis exists in the real world.How should it be modeled?

What Is Metamorphosis?

Metamorphosis 1. A change in form, structure, or function;

specifically the physical change undergone by some animals, as of the tadpole to the frog.

2. Any marked change, as in character, appearance, or condition.

~ Webster’s New World Dictionary, Simon & Schuster, Inc., 1979

Page 92: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi92

Example: Metamorphosis

In the university, there are full-time students and part-time students Full-time students have an expected graduation date

but part-time students do not Part-time students may take a maximum of three

courses but there is no maximum for full-time students

FullTimeStudent+ name+ address+ studentID+ gradDate

PartTimeStudent+ name+ address

+ maxNumCourses+ studentID

Page 93: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi93

What happens if a part-time student

becomes a full-time student?

Modeling Metamorphosis: One Approach

A generalization relationship may be created

FullTimeStudent

Student+ name+ address+ studentID

+ gradDate

PartTimeStudent

+ maxNumCourses

Page 94: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi94

Inheritance may be used to model common structure, behavior, and/or relationships to the “changing” parts

Modeling Metamorphosis: Another Approach

Student+ name+ address+ studentID

FullTimeStudent

+ gradDate

PartTimeStudent

+ maxNumCourses

Student+ name+ address+ studentID

FullTimeClassification

+ gradDate

PartTimeClassification

+ maxNumCourses

Classification1

1

Page 95: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi95

Modeling Metamorphosis: Another Approach (cont.)

Metamorphosis is accomplished by the object “talking” to the changing parts

: Student : FullTimeClassification : StudentManager : PartTimeClassification

2 : \delete\

3 : \create\

1 : \changeToFullTime\

Page 96: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi96

Metamorphosis and Flexibility

This technique also adds to the flexibility of the model

studentID1

1ResidentInformation

1

0..1 Classification

ParttimeClassification+ maxNumCourses

FulltimeClassification+ gradDate

+ dorm+ room+ roomKeyID

Student+ name+ address+ studentID

Page 97: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi97

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 98: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi98

Resolve Use-Case Collisions

Multiple use cases may simultaneously access design objects

Options Use synchronous messaging => first-come first-

serve order processing Identify operations (or code) to protect Apply access control mechanisms

• Message queuing• Semaphores (or “tokens”)• Other locking mechanism

Resolution is highly dependent on implementation environment

Page 99: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi99

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 100: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi100

Handle Non-Functional Requirements in General

Analysis Class Analysis Mechanism(s)

Student

Schedule

CourseOffering

Course

RegistrationController

Persistency, Security

Persistency, Legacy Interface

Persistency, Legacy Interface

Distribution

Persistency, Security

DesignGuidelines

Analysis Design Implementation

Remote Method Invocation (RMI)

Persistency

Analysis

Mechanism

(Conceptual)

Design

Mechanism

(Concrete)

Implementation

Mechanism

(Actual)

OODBMS

RDBMS

JDBC

ObjectStore

Java 1.2 from Sun

Legacy Data

New Data

Distribution

Persistency

SomeDesignClass

Page 101: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi101

Class Design Steps

Create Initial Design Classes Define Operations Define Methods Define States Define Attributes Define Dependencies Define Associations Define Generalizations Resolve Use-Case Collisions Handle Non-Functional Requirements in General Checkpoints

Page 102: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi102

Checkpoints: Classes

Clear class names One well-defined abstraction Functionally coupled attributes/behavior Generalizations were made. All class requirements were addressed Demands are consistent with statecharts. Complete class instance life cycle is described. The class has the required behavior.

Page 103: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi103

Checkpoints: Operations

Operations are easily understood State description is correct Required behavior is offered Parameters are defined correctly Messages are completely assigned operations Implementation specifications are correct Signatures conform to standards All operations are needed by Use-Case

Realizations

Page 104: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi104

Checkpoints: Attributes

A single concept Descriptive names All attributes are needed by Use-

Case Realizations

Page 105: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi105

Checkpoints: Relationships

Descriptive role names Correct multiplicities

Page 106: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi106

Review: Class Design

What is the purpose of Class Design? In what ways are classes refined? Are statecharts created for every class? What are the major components of a

statechart? Provide a brief description of each.

What kind of relationship refinements occur? What is the difference between an

association and a dependency? What is done with operations and attributes?

Page 107: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi107

(continued)

Exercise 2: Class Design

Given the following: The Use-Case Realization for a use

case and/or the detailed design of a subsystem

The design of all participating design elements

Page 108: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi108

Exercise 2: Class Design (cont.)

Identify the following: The required navigability for each

relationship Any additional classes to support the

relationship design Any associations refined into

dependencies Any associations refined into

aggregations or compositions Any refinements to multiplicity Any refinements to existing

generalizations Any new applications of generalization

• Make sure any metamorphosis is considered (continued)

Page 109: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi109

Exercise 2: Class Design (cont.)

Produce the following: An updated VOPC, including the

relationship refinements (generalization, dependency, association)

(continued)

Page 110: Mastering Object-Oriented Analysis and Design with UML Module 13: Class Design

OOAD - Dr. A. Alghamdi110

Exercise 2: Review

Compare your resultsDo your dependencies represent

context independent relationships?

Are the multiplicities on the relationships correct?

Does the inheritance structure capture common design abstractions, and not implementation considerations?

Is the obvious commonality reflected in the inheritance hierarchy?

Payroll System