basic class design example: payroll purpose: picture the object-creation, message-passing interplay...
TRANSCRIPT
![Page 1: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/1.jpg)
Basic Class Design
![Page 2: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/2.jpg)
Example: Payroll
Purpose:
Picture the object-creation, message-passing interplay of objects in an executing program.
Statement of The Problem:
Given a source of time card information and a personnel file, print payroll checks
![Page 3: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/3.jpg)
What Objects Would You Expect?
Statement of The Problem (again):
Given a source of time card information
and a personnel file, print payroll checks
Common heuristic: look for the nouns.
![Page 4: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/4.jpg)
Payroll Classes
• PayrollMaster• Employee• PersonnelFile• TimeCard
• TimeCardFile• Check• SS#• Hours
![Page 5: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/5.jpg)
Payroll Objects
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookup
SSmatch
TimegetHours
Employee
PayYourself
Checkprin
t
Programmain
![Page 6: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/6.jpg)
TimeCard File
getCard
Payroll MasterDoPayroll
Payroll MasterDoPayroll
PersonnelFile
lookup
TimeCard File
getCard
Payroll Program (1)
Programmain
Payroll MasterDoPayroll
PersonnelFile
lookup
PersonnelFile
lookup
TimeCard File
getCard
![Page 7: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/7.jpg)
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookup
TimeCard
getSSgetTim
e
TimeCard
getSSgetTim
e
TimeCard
getSSgetTim
e
PayrollMaster: DoPayroll (1)
![Page 8: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/8.jpg)
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookupget SS
SSmatchSSmatch
get SS
SSmatch
get SS
PayrollMaster: DoPayroll (2)
get SS
SSmatch
![Page 9: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/9.jpg)
get Time
TimegetHours
get Timeget Time
TimegetHours
PayrollMaster: DoPayroll (3)
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookup
SSmatch
get Time
TimegetHoursTimegetHoursTimegetHours
![Page 10: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/10.jpg)
Personnel: Lookup
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookup
SSmatch
TimegetHours
Employee
PayYourselfEmployee
PayYourselfEmployee
PayYourself
![Page 11: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/11.jpg)
PayrollMaster: DoPayroll (4)
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll Personn
elFilelookup
SSmatch
TimegetHours
Employee
PayYourself
Checkprin
t
Checkprin
t
Checkprin
t
Checkprin
t
Checkprin
t
Checkprin
t
![Page 12: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/12.jpg)
Payroll Program (2)
TimeCard
getSSgetTim
e
TimeCard File
getCard
Payroll MasterDoPayroll
PersonnelFile
lookup
SSmatch
TimegetHours
Employee
PayYourself
Checkprin
t
Programmain
![Page 13: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/13.jpg)
A Peculiar Employee Design?
• Since when do employees create their own
checks?
• Since when are employees trusted to determine
their own pay?
(not in OUR contract!)
SMART and HELPFUL Objects
![Page 14: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/14.jpg)
OOP and O-O Design
• Can't rush in and “program”
• Design phase needed even in simple problems– desirable in procedural programming– sine qua non in O-O world
• O-O design methodologies:– numerous– we'll consider just one simple one
![Page 15: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/15.jpg)
Kindergarten OODStatement of
Problem
Possible Objects
Primary Object
Behavior
Interface
Sample Use
Implement
![Page 16: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/16.jpg)
Kindergarten OOD
• Not appropriate for all problems
• Works surprisingly well on many– great for CS1
• Illustrate with an example:
Find the value of a portfolio of stocks based on “ticker-tape” information
![Page 17: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/17.jpg)
Statement of Problem:
Find the value of a portfolio of stocksbased on "ticker-tape" information.
![Page 18: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/18.jpg)
Possible Objects:
• Portfolio
• Holding (a portfolio item)
• Value
• TickerTape
![Page 19: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/19.jpg)
Primary Object?
• not Holding
• not Value
• Portfolio vs. TickerTape?
![Page 20: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/20.jpg)
Portfolio vs. TickerTape?
• Both primary in the sense of independence
• But to which object should we send a messageto solve our problem?
![Page 21: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/21.jpg)
Is Portfolio the Primary Object?
• If so, Portfolio has this behavior:
get Value of Portfolio, given TickerTape
![Page 22: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/22.jpg)
Is TickerTape the Primary Object?
• If so, TickerTape has this behavior:– get Value of Portfolio, given Portfolio
• Should TickerTape be responsible for computing the Value of a Portfolio?
![Page 23: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/23.jpg)
Responsibility-Driven Design:
• objects should be responsible for themselves
• example:– objects should be responsible for their own
initialization (constructors)
• TickerTape should not be responsible for Value of a Portfolio, Portfolio should
• Primary Object:Portfolio
![Page 24: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/24.jpg)
Behavior of Portfolio:
• get Value, given TickerTape
• constructor, given a file listing of a portfolio
![Page 25: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/25.jpg)
Interface of Portfolio:
class Portfolio {Portfolio create()Value getValue(TickerTape t)
}
![Page 26: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/26.jpg)
Sample Use:
create TickerTape tcreate Portfolio pto p: get Value v, given tto v: print
![Page 27: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/27.jpg)
Implementing Portfolio:class Portfolio {
constructor() {}
Value getValue(TickerTape t) {}
}
![Page 28: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/28.jpg)
Implementing Portfolio: getValue (1):class Portfolio {
Value getValue(TickerTape t) {for each Holding h in Collection c of Holdings {
get the Value of hincrease the total by v
}return total
}
}•Needed:
•STATE: Collection object, c
A Portfolio HAS-A collection of holdings•Local Value object, total
![Page 29: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/29.jpg)
Implementing Portfolio: getValue (2):class Portfolio {
Value getValue(TickerTape t) {Value total initialized to 0
for each Holding h in Collection c of Holdings {get the Value of hincrease the total by v
}return total
}
STATE: Collection c}
![Page 30: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/30.jpg)
Getting The Value of a Holding (1)
• This is OOP
• Ya want something done, send an object a
message
to h: getValue
![Page 31: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/31.jpg)
Getting The Value of a Holding (2)to h: getValue
• Problem: a Holding is responsible for • name of stock
• number of shares
• NOT current market value
• Solution: send h an object that has the necessary behavior
to h: getValue given TickerTape t
![Page 32: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/32.jpg)
Increasing a Value
• This is OOP
• Ya want something done, send an object a
message
to total: increase by ...
![Page 33: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/33.jpg)
Implementing Portfolio: getValue (3):class Portfolio {
Value getValue(TickerTape t) {Value total initialized to 0for each Holding h in Collection c of Holdings {
to h: get the Value v of your stock, given tto total: increase by v
}return total
}
STATE: Collection c}
• Remark: Who initializes the Collection?
![Page 34: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/34.jpg)
The Portfolio Constructor (1)class Portfolio {
constructor() {create Collection c... ???
}
...
STATE: Collection c
}
![Page 35: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/35.jpg)
Constructor Questions and Answers• Questions:
– where will the portfolio data come from?– in what form will it be?
• Answers (let's say):– from a TokenSource– a sequence of (stock name and number) pairs
• Question:– Where should the responbility for creating TokenSource
lie?
![Page 36: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/36.jpg)
The Portfolio Constructor (2)class Portfolio {
constructor(TokenSource src) {create Collection ccreate Holding h, given srcwhile h was created successfully {
to c: add hcreate Holding h, given src
}}…
STATE: Collection c}
![Page 37: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/37.jpg)
Portfolio Class:class Portfolio {
constructor(TokenSource src) {create Collection ccreate Holding h, given srcwhile h was created successfully {
to c: add hcreate Holding h, given src
}}Value getValue(TickerTape t) {
Value total initialized to 0for each Holding h in Collection c of Holdings { to h: get the Value v of your stock, given t to total: increase by v}return total
}
STATE: Collection c}
![Page 38: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/38.jpg)
Class Summary
Completed:– Portfolio
Yet To Complete:– TickerTape
– TokenSource
– Collection
– Holding
– Value
![Page 39: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/39.jpg)
Details of Remaining Classes:• TickerTape— constructor• TokenSource — constructor
get String• Collection— constructor
add... some way of iterating …
• Holding— constructor, given TokenSourceget Value given TickerTape
• Value— constructor, given an integer valueincrease, given a Valueprint
![Page 40: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/40.jpg)
NEXT STEP: Pick a class and follow same procedure…
• TickerTape
• TokenSource
• Collection
• Holding
• Value
![Page 41: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/41.jpg)
Statement of SubProblem…
… skip steps because
behavior
interface
have been determined
![Page 42: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/42.jpg)
Behavior of Holding:
• create, given TokenSource
• get Value given TickerTape
![Page 43: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/43.jpg)
Interface of Holding:
class Holding {
constructor(TokenSource src)
Value getValue(TickerTape t)
}
![Page 44: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/44.jpg)
Sample Use:
... from implementation of Portfolio
![Page 45: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/45.jpg)
Implementing Holding:
class Holding {
constructor(TokenSource src) {}
Value getValue(TickerTape t) }}
}
![Page 46: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/46.jpg)
Implementing Holding: getValueclass Holding {
Value getValue(TickerTape t) {to t: get Value v of stock named by my StockNameto v: multiply by my Numberreturn v
}}
• Remarks:– the object must maintain a StockName and a Number,
call them sn and n
![Page 47: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/47.jpg)
Implementing Holding: STATE
class Holding {
Value getValue(TickerTape t) {to t: get Value v given snto v: multiply by nreturn v
}
STATE:StockName snNumber n
}
![Page 48: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/48.jpg)
Implementing Holding: constructorclass Holding {
constructor(TokenSource src) {create StockName sn, given srccreate Number n, given src
}
...
STATE:StockName snNumber n
}
• Remark:Each class bears the responsibility for doingmost of the work to create its own instances
![Page 49: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/49.jpg)
The Holding Classclass Holding {
constructor(TokenSource src) {create StockName sn, given srccreate Number n, given src
}
Value getValue(TickerTape t) {to t: get Value v given snto v: multiply by nreturn v
}
STATE:StockName snNumber n
}
![Page 50: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/50.jpg)
Class Summary
Completed:– Portfolio
– Holding
Yet To Complete:– TickerTape
– TokenSource
– Collection
– Value
– Number
– StockName
![Page 51: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/51.jpg)
Details of Remaining Classes (I):• TickerTape— constructor
– get Value given a StockName
• TokenSource — constructor– get String
• Collection— constructor– add– ... some way of iterating …
NEW!
![Page 52: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/52.jpg)
Details of Remaining Classes (II):
• Value— constructor, given an integer value– increase, given a Value– multiply, given a Number– print
• Number— constructor
• StockName— constructor
NEW!
NEW!
![Page 53: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/53.jpg)
NEXT STEP: Pick a class and follow procedure....
• TickerTape
• TokenSource
• Collection
• Value
• Number
• StockName
![Page 54: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/54.jpg)
Statement of SubProblem:
• ... skip steps because behavior, interface has been determined
![Page 55: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/55.jpg)
Behavior of TickerTape:
• Create
• get Value given StockName
![Page 56: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/56.jpg)
Interface of TickerTape:
class TickerTape {
constructor()
TickerTape getValue(StockName sn)
}
![Page 57: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/57.jpg)
Sample Use:
... from implementation of Holding
![Page 58: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/58.jpg)
Implementing TickerTapeclass TickerTape {
constructor() {}
Value getValue(StockName sn) {}
}
![Page 59: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/59.jpg)
Implementing TickerTape: getValue
Remark: need a Collection in my state
class TickerTape {
Value getValue(StockName sn) {for each StockQuote s
in my Collection c of StockQuotes {to s: does sn match?if yes {
to s: getValue vreturn v
}}error
}}
![Page 60: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/60.jpg)
Implementing TickerTape: Stateclass TickerTape {
Value getValue(StockName sn) {for each StockQuote s
in my Collection c of StockQuotes {to s: does sn match?if yes {
to s: getValue vreturn v
}}error
}
STATE:Collection c}
![Page 61: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/61.jpg)
Implementing TickerTape: constructorclass TickerTape {
constructor(TokenSource src) {create Collection ccreate StockQuote s given srcwhile not failure {
to c: add screate StockQuote s given src
}}
...
STATE:Collection c}
![Page 62: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/62.jpg)
The TickerTape Classclass TickerTape {
constructor(TokenSource src) {create Collection ccreate StockQuote s given srcwhile not failure {
to c: add screate StockQuote s given src
}}
![Page 63: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/63.jpg)
The TickerTape Class (continued)
Value getValue(StockName sn) {for each StockQuote s
in my Collection c of StockQuotes {to s: does sn match?if yes {
to s: getValue vreturn v
}}error}STATE:Collection c
}
![Page 64: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/64.jpg)
Class Summary
Completed:– Portfolio
– Holding
– TickerTape
Yet To Complete:– TokenSource
– Collection
– Value
– Number
– StockName
– StockQuote
![Page 65: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/65.jpg)
Details of Remaining Classes (I):• TokenSource — constructor
– get String• Collection— constructor
– add– ... some way of iterating …
• Value— constructor, given an integer value– increase, given a Value– multiply, given a Number– print
![Page 66: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/66.jpg)
Details of Remaining Classes (II):
• Number— constructor
• StockName— constructor
• StockQuote— constructor, given a TokenSource– match a StockName– return the Value of a stock
NEW!
![Page 67: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/67.jpg)
NEXT STEP: Pick a class and follow procedure....
• TickerTape
• TokenSource
• Collection
• Value
• Number
• StockName
• StockQuote
![Page 68: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/68.jpg)
Statement of SubProblem:
... skip steps because behavior, interface has been determined
![Page 69: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/69.jpg)
Behavior of StockQuote:
• create, given a TokenSource
• get Value
• is match? given a StockName
![Page 70: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/70.jpg)
Interface of StockQuote:
class StockQuote {
constructor(TokenSource src)
Value getValue()
boolean isMatch(StockName sn)
}
![Page 71: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/71.jpg)
Sample Use:
• ... from implementation of TickerTape
![Page 72: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/72.jpg)
Implementing StockQuote
class StockQuote {constructor(TokenSource src) {
}
StockQuote getValue() {
}
boolean isMatch(StockName sn) {
}
}
![Page 73: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/73.jpg)
Implementing StockQuote (I)class StockQuote {
constructor(TokenSource src) {}
StockQuote getValue() {return v;
}
boolean isMatch(StockName sn) {to sn: does my StockName sn match?if yes return true else return false
}
STATE:Value vStockName sn
}
![Page 74: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/74.jpg)
Implementing StockQuote (II)class StockQuote {
constructor(TokenSource src) {create StockName sn, given srccreate Value v, given src
}
...
STATE:Value vStockName sn
}
![Page 75: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/75.jpg)
The StockQuote Classclass StockQuote {
constructor(TokenSource src) {create StockName sn, given srccreate Value v, given src
}
StockQuote getValue() {return v;
}
boolean isMatch(StockName sn) {to sn: does my StockName sn match?if yes return true else return false
}
STATE:Value vStockName sn
}
![Page 76: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/76.jpg)
Class Summary
Completed:– Portfolio
– Holding
– TickerTape
– StockQuote
Yet To Complete:– TokenSource
– Collection
– Value
– Number
– StockName
![Page 77: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/77.jpg)
Details of Remaining Classes (I):• TokenSource — constructor
– get String• Collection— constructor
– add– ... some way of iterating …
• Value— constructor, given an integer value– increase, given a Value– multiply, given a Number– print
![Page 78: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/78.jpg)
Details of Remaining Classes (II):
• Number— constructor
• StockName— constructor– is Match? given another StockName
NEW!
![Page 79: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/79.jpg)
NEXT STEP: Pick a class and follow procedure....
• TickerTape
• TokenSource
• Collection
• Value
• Number
• StockName
![Page 80: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/80.jpg)
Statement of SubProblem:
• ... skip steps because behavior, interface has been determined
![Page 81: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/81.jpg)
Behavior of Value:
• create, given a TokenSource
• increase, given a Value
• multiply, given a Number
![Page 82: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/82.jpg)
Interface of Value:
class Value {
Value create(TokenSource src)
void increase(Value v)
void multiply(Number n)
void print()
}
![Page 83: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/83.jpg)
Sample Use:
... from implementation of TickerTape
![Page 84: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/84.jpg)
Implementing Value
class Value {
Value create(TokenSource src) {
}
void increase(Value v) {
}
void multiply(Number n) {
}
void print() {
}
}
![Page 85: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/85.jpg)
Implementing Value: constructor
class Value {constructor(TokenSource src) {
to src: get String integerPartto src: get String fractionto fraction: contains / ?if no
to src: put back String fraction// just use integerPart
else// use integerPart and fraction
}...
}
![Page 86: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/86.jpg)
Implementing Value: other methodsclass Value {
Value create(TokenSource src) {...
}
void increase(Value v) {// defer
}
void multiply(Number n) {// defer
}
void print() {// defer
}
STATE: ???}
![Page 87: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/87.jpg)
Class Summary
Completed:– Portfolio
– Holding
– TickerTape
– StockQuote
– Value (partially)
Yet To Complete:– TokenSource
– Collection
– Number
– StockName
![Page 88: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/88.jpg)
Details of Remaining Classes:• TokenSource — constructor
– get String– put back String, given String
• Collection— constructor– add– ... some way of iterating …
• Number— constructor
• StockName— constructor– is Match? given another StockName
NEW!
![Page 89: Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of](https://reader036.vdocument.in/reader036/viewer/2022062515/56649cc15503460f949886a0/html5/thumbnails/89.jpg)
NEXT STEP:
And so on …
(I'm tired now— aren't you?)