Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose...

Post on 19-Jan-2016

213 views 0 download

Tags:

Transcript of Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose...

Object-Oriented Design

2

3

• Objects and concerns• Objects have a concern, meaning they have

a purpose• Not concerned as in worried

• All code should have a purpose

Why is that joke supposed to be funny?

4

Classes

• Each class should have a clear purpose• One class usually corresponds to one kind of entity• Each class member corresponds to one attribute

• Only code related to that purpose goes in class• Functions for data that they modify• Code that needs to be modified at the same time

5

Software Packages

• Every software package should have a purpose• Code in software package iff relates to purpose

• (iff -> if and only if)

• “Module” can refer to either a class or package• Every module should have a purpose• Code in module iff relates to purpose

6

Example: Drug and Alcohol Counseling

7

Survey DB

Survey

Surveyanswers

HealthInformation

All thispatient’s

answers (ever)

Counselee

Counselor

Create report

PostscriptPrinterPick up

Printout

Printout

Authenticat

e

User ID

Last name & PIN

• What are the key concerns?

8

Key Concerns of Counseling Example

• Managing the users

• Performing the survey

• Generating the report

9

Key Concerns of Counseling Example

• Managing the users• Authenticating counselees• Matching counselees to counselors

• Performing the survey• Generating the report

10

Key Concerns of Counseling Example

• Managing the users

• Performing the survey• Representing the questions• Representing the answers• Storing answers• Allowing skips

• Generating the report

11

Key Concerns of Counseling Example

• Managing the users

• Performing the survey

• Generating the report• Reading the data• Performing calculations of the report• Sending to printer

12

UML Diagram (Review)

• One box per entity• Usually lists attributes• Interfaces and abstractions italicized

• Lines without arrowheads show references• Represents member variables in OO• Labeled with cardinality

• Lines with open arrowheads for specialization• Lines with regular arrowheads for dependencies

13

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Simple UML Diagram

note: UML details omitted for clarity

14

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Organize into Packages

Data Records

Authenticator

Survey

Report

15

Coupling and Cohesion

• Coupling• When one module is involved in another module’s concern

• Cohesion• When a module is devoted to its own concern

16

Levels of Coupling

• Content (worst)• A modifies B

• Common• Control• Stamp• Data• Uncoupled (best)

Coupling reduces maintainability!

A Bmodifies

17

Levels of Coupling

• Content (worst)• Common

• A and B read/write same data

• Control• Stamp• Data• Uncoupled (best)

Coupling reduces maintainability!

A B

data

read/write

18

Levels of Coupling

• Content (worst)• Common• Control

• A calls B

• Stamp• Data• Uncoupled (best)

Coupling reduces maintainability!

A

Bcalls

19

Levels of Coupling

• Content (worst)• Common• Control• Stamp

• A provides structured data to B

• Data• Uncoupled (best)

Coupling reduces maintainability!

A

B

structured data

20

Levels of Coupling

• Content (worst)• Common• Control• Stamp• Data

• A provides unstructured data to B

• Uncoupled (best)

Coupling reduces maintainability!

A

B

unstructured data

21

Levels of Coupling

• Content (worst)• Common• Control• Stamp• Data• Uncoupled (best)

• None of the above

Coupling reduces maintainability!

AB

22

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Inter-package Couplings?

Data Records

Authenticator

Survey

Report

content: A modifies Bcommon: A / B read/write same datacontrol: A calls Bstamp: A -> B structured datadata: A->B unstructured datauncoupled

23

Levels of Cohesion

• Functional / informational (best) • A and B work together for one purpose

• Communicational• Procedural• Temporal• Logical• Coincidental (worst)

Cohesion increases maintainability

A B

one purpose

24

Levels of Cohesion

• Functional / informational (best) • Communicational

• A and B use the same data

• Procedural• Temporal• Logical• Coincidental (worst)

Cohesion increases maintainability

A B

data

25

Levels of Cohesion

• Functional / informational (best) • Communicational• Procedural

• A executes, then B executes…• A & B have vaguely related purpose

• Temporal• Logical• Coincidental (worst)

Cohesion increases maintainability

A B

purpose

time

26

Levels of Cohesion

• Functional / informational (best) • Communicational• Procedural• Temporal

• A executes, then B executes…• A & B have NO related purpose

• Logical• Coincidental (worst)

Cohesion increases maintainability

A B

time

27

Levels of Cohesion

• Functional / informational (best) • Communicational• Procedural• Temporal• Logical

• Either A or B might be executed

• Coincidental (worst)

Cohesion increases maintainability

A B

A ∨ B

28

Levels of Cohesion

• Functional / informational (best) • Communicational• Procedural• Temporal• Logical• Coincidental (worst)

• None of the above

Cohesion increases maintainability

A

B

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Intra-package Cohesion?

Functional: A&B have one purposeCommunicational: A&B share same dataProcedural: A runs, then B, relatedTemporal: A runs, then B, unrelated Logical: either A or B might be executedNo cohesion

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Tip #1: “Don’t talk to strangers”

This would be bad:

Counselee Rec.

Counselor Rec.

Survey Instance

Questions

Answers

Authenticator

Report Maker

Survey Server

Question loader

Skip logic module

Answer storer

Data loader

Calculation module

Printer controller

Tip #2: Move Code to Where It’s Used

Reduce inter-package coupling

32

Tip #3: Split Modules to Reduce Cycles

• Our example had no cycles• But here’s a way to correct them:

33

Tip #4: In Reuse, Prefer Composition over Inheritance

• Use composition to add features or to reuse code• Use inheritance to add a new version of an entity

Survey Instance

Questions

Answers

Survey Server

Question loader

Skip logic module

Answer storerTiming data

Answer check

Logging module

Tip #4: In Reuse, Prefer Composition over Inheritance

• Use composition to add features or to reuse code• Use inheritance to add a new version of an entity

Survey Instance

Questions Answers

numeric question

multiple choice question

free-text question

numeric answer

multiple choice answer

free-text answer

35

Interface and Polymorphism

• An interface is a promise:• I can provide this output if you provide a valid input• If you can meet these preconditions, then I can meet these

postconditions

• Polymorphism: If A, B, C, and D provide same interface, then they all make the same promise• May keep the promise in different ways

36

Incremental vs. Iterative

• Use incremental development when:• Much of the system’s value resides in one subsection• One part of the system must be completed (logically) before another

• Use iterative development when:• System’s value is spread out over much of the system• The whole system needs to work before you can build it up

37

Incremental Examples

• Adding new kinds of print outs• Printout carries much of system’s value

• Adding a new data export module• Logically, main system needs to work before worrying about

exporting data

38

Iterative Examples

• Tweaking reports and user interface (surveyor) to improve usability• Improvements to existing system pieces

• Adding new kinds of questions (and answers), changing reports• Changes spread across system