Top Banner
Object-Oriented Design
38

Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

Jan 19, 2016

Download

Documents

Scott Lane
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

Object-Oriented Design

Page 2: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

2

Page 3: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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?

Page 4: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 5: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 6: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

6

Example: Drug and Alcohol Counseling

Page 7: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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?

Page 8: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

8

Key Concerns of Counseling Example

• Managing the users

• Performing the survey

• Generating the report

Page 9: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

9

Key Concerns of Counseling Example

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

• Performing the survey• Generating the report

Page 10: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 11: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 12: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 13: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 14: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 15: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 16: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

16

Levels of Coupling

• Content (worst)• A modifies B

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

Coupling reduces maintainability!

A Bmodifies

Page 17: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 18: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

18

Levels of Coupling

• Content (worst)• Common• Control

• A calls B

• Stamp• Data• Uncoupled (best)

Coupling reduces maintainability!

A

Bcalls

Page 19: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 20: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 21: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

21

Levels of Coupling

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

• None of the above

Coupling reduces maintainability!

AB

Page 22: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 23: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 24: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 25: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 26: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 27: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 28: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

28

Levels of Cohesion

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

• None of the above

Cohesion increases maintainability

A

B

Page 29: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 30: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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:

Page 31: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 32: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

32

Tip #3: Split Modules to Reduce Cycles

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

Page 33: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 34: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 35: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 36: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 37: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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

Page 38: Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a.

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