Object-Oriented Design

advertisement
Object-Oriented Design
1
Why is that joke
supposed to be
funny?
• Objects and concerns
• Objects have a concern, meaning they have
a purpose
• Not concerned as in worried
• All code should have a purpose
2
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
3
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
4
Example: Drug and Alcohol Counseling
5
Last name
& PIN
Authen
ticate
Counselee
User ID
Survey DB
• What are the key concerns?
All this
patient’s
answers (ever)
Printout
Pick up
Printer
Printout
6
Counselor
Survey
answers
Survey
Health
Information
Postscript
Create
report
Key Concerns of Counseling Example
• Managing the users
• Performing the survey
• Generating the report
7
Key Concerns of Counseling Example
• Managing the users
• Authenticating counselees
• Matching counselees to counselors
• Performing the survey
• Generating the report
8
Key Concerns of Counseling Example
• Managing the users
• Performing the survey
•
•
•
•
Representing the questions
Representing the answers
Storing answers
Allowing skips
• Generating the report
9
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
10
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
11
Simple UML Diagram
Authenticator
note: UML details omitted for clarity
Survey Server
Counselee Rec.
Counselor Rec.
Question loader
Skip logic module
Answer storer
Survey Instance
Questions
Answers
Report Maker
Data loader
Calculation module
12
Printer controller
Organize into Packages
Authenticator
Authenticator
Survey Server
Counselee Rec.
Counselor Rec.
Question loader
Survey
Skip logic module
Answer storer
Instance
DataSurvey
Records
Questions
Answers
13
Report Maker
Data loader
Report
Calculation module
Printer controller
Coupling and Cohesion
• Coupling
• When one module is involved in another module’s concern
• Cohesion
• When a module is devoted to its own concern
14
Levels of Coupling
• Content (worst)
• A modifies B
•
•
•
•
•
Common
Control
Stamp
Data
Uncoupled (best)
A
Coupling reduces maintainability!
15
modifies
B
Levels of Coupling
• Content (worst)
• Common
data
• A and B read/write same data
•
•
•
•
Control
Stamp
Data
Uncoupled (best)
Coupling reduces maintainability!
16
read/write
A
B
Levels of Coupling
• Content (worst)
• Common
• Control
• A calls B
• Stamp
• Data
• Uncoupled (best)
Coupling reduces maintainability!
17
B
A
calls
Levels of Coupling
•
•
•
•
Content (worst)
Common
Control
Stamp
A
• A provides structured data to B
• Data
• Uncoupled (best)
Coupling reduces maintainability!
18
structured data
B
Levels of Coupling
•
•
•
•
•
Content (worst)
Common
Control
Stamp
Data
A
• A provides unstructured data to B
unstructured data
• Uncoupled (best)
Coupling reduces maintainability!
19
B
Levels of Coupling
•
•
•
•
•
•
Content (worst)
Common
Control
Stamp
Data
Uncoupled (best)
A
• None of the above
Coupling reduces maintainability!
20
B
Inter-package Couplings?
content: A modifies B
common: A / B read/write same data
control: A calls B
stamp: A -> B structured data
data: A->B unstructured data
uncoupled
Authenticator
Authenticator
Survey Server
Question loader
Counselee Rec.
Counselor Rec.
Instance
DataSurvey
Records
Questions
Answers
21
Survey
Skip logic module
Answer storer
Report Maker
Data loader
Report
Calculation module
Printer controller
Levels of Cohesion
• Functional / informational (best)
• A and B work together for one purpose
•
•
•
•
•
Communicational
Procedural
Temporal
Logical
Coincidental (worst)
Cohesion increases maintainability
22
one
purpose
A
B
Levels of Cohesion
• Functional / informational (best)
• Communicational
data
• A and B use the same data
•
•
•
•
Procedural
Temporal
Logical
Coincidental (worst)
Cohesion increases maintainability
23
A
B
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
24
purpose
A
B
time
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
25
A
B
time
Levels of Cohesion
•
•
•
•
•
Functional / informational (best)
Communicational
Procedural
Temporal
Logical
A∨B
• Either A or B might be executed
• Coincidental (worst)
Cohesion increases maintainability
26
A
B
Levels of Cohesion
•
•
•
•
•
•
Functional / informational (best)
Communicational
Procedural
Temporal
Logical
Coincidental (worst)
• None of the above
Cohesion increases maintainability
27
A
B
Intra-package Cohesion?
Functional: A&B have one purpose
Communicational: A&B share same data
Procedural: A runs, then B, related
Temporal: A runs, then B, unrelated
Logical: either A or B might be executed
No cohesion
Authenticator
Survey Server
Question loader
Counselee Rec.
Counselor Rec.
Survey Instance
Questions
Answers
Skip logic module
Answer storer
Report Maker
Data loader
Calculation module
Printer controller
Tip #1: “Don’t talk to strangers”
This would be bad:
Authenticator
Survey Server
Question loader
Counselee Rec.
Counselor Rec.
Survey Instance
Questions
Answers
Skip logic module
Answer storer
Report Maker
Data loader
Calculation module
Printer controller
Tip #2: Move Code to Where It’s Used
Reduce inter-package coupling
Authenticator
Survey Server
Question loader
Counselee Rec.
Skip logic module
Answer storer
Counselor Rec.
Report Maker
Survey Instance
Data loader
Questions
Calculation module
Answers
Printer controller
Tip #3: Split Modules to Reduce Cycles
• Our example had no cycles
• But here’s a way to correct them:
31
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 storer
Timing data
Answer check
32
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
numeric answer
multiple choice
question
multiple choice
answer
free-text question
free-text answer
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
34
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
35
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
36
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
37
Download