Download presentation
Presentation is loading. Please wait.
Published byScott Lane Modified over 9 years ago
1
Object-Oriented Design
2
1
3
Objects and concerns Objects have a concern, meaning they have a purpose Not concerned as in worried All code should have a purpose 2 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 3
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 4
6
5 Example: Drug and Alcohol Counseling
7
6 Survey DB Survey Survey answers Health Information All this patient’s answers (ever) Counselee Counselor Create report Postscript Printer Pick up Printout Printout Authen ticate 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 7
9
Key Concerns of Counseling Example Managing the users Authenticating counselees Matching counselees to counselors Performing the survey Generating the report 8
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 9
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 10
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 11
13
12 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
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 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 14
16
Levels of Coupling Content (worst) A modifies B Common Control Stamp Data Uncoupled (best) Coupling reduces maintainability! 15 A A B B modifies
17
Levels of Coupling Content (worst) Common A and B read/write same data Control Stamp Data Uncoupled (best) Coupling reduces maintainability! 16 A A B B data read/write
18
Levels of Coupling Content (worst) Common Control A calls B Stamp Data Uncoupled (best) Coupling reduces maintainability! 17 A A B B calls
19
Levels of Coupling Content (worst) Common Control Stamp A provides structured data to B Data Uncoupled (best) Coupling reduces maintainability! 18 A A B B structured data
20
Levels of Coupling Content (worst) Common Control Stamp Data A provides unstructured data to B Uncoupled (best) Coupling reduces maintainability! 19 A A B B unstructured data
21
Levels of Coupling Content (worst) Common Control Stamp Data Uncoupled (best) None of the above Coupling reduces maintainability! 20 A A B B
22
21 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 B common: A / B read/write same data control: A calls B stamp: A -> B structured data data: A->B unstructured data uncoupled
23
Levels of Cohesion Functional / informational (best) A and B work together for one purpose Communicational Procedural Temporal Logical Coincidental (worst) Cohesion increases maintainability 22 A A B B one purpose 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 23 A A B 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 24 A A B 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 25 A A B 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 26 A A B B A ∨ B
28
Levels of Cohesion Functional / informational (best) Communicational Procedural Temporal Logical Coincidental (worst) None of the above Cohesion increases maintainability 27 A A B B
29
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 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
30
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:
31
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: 31
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 32 Survey Instance Questions Answers Survey Server Question loader Skip logic module Answer storer Timing data Answer check Logging module
34
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 multiple choice question free-text question numeric answer multiple choice 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 34
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 35
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 36
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 37
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.