Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose...
-
Upload
scott-lane -
Category
Documents
-
view
213 -
download
0
Transcript of Object-Oriented Design. 1 Objects and concerns Objects have a concern, meaning they have a purpose...
![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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/1.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/2.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/3.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/4.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/5.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/6.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/7.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/8.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/9.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/10.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/11.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/12.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/13.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/14.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/15.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/16.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/17.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/18.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/19.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/20.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/21.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/22.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/23.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/24.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/25.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/26.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/27.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/28.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/29.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/30.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/31.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/32.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/33.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/34.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/35.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/36.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/37.jpg)
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.](https://reader036.fdocuments.in/reader036/viewer/2022070415/5697c0281a28abf838cd6e55/html5/thumbnails/38.jpg)
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