1 / 89

Basic Class Design

Basic Class Design. 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. What Objects Would You Expect?.

Télécharger la présentation

Basic Class Design

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Basic Class Design

  2. 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

  3. 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.

  4. PayrollMaster Employee PersonnelFile TimeCard TimeCardFile Check SS# Hours Payroll Classes

  5. Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Objects

  6. Program PersonnelFile PersonnelFile PersonnelFile main lookup lookup lookup TimeCard File TimeCard File TimeCard File getCard getCard getCard Payroll Master Payroll Master Payroll Master DoPayroll DoPayroll DoPayroll Payroll Program (1)

  7. PersonnelFile lookup TimeCard TimeCard TimeCard TimeCard File getTime getTime getTime getSS getSS getSS getCard Payroll Master DoPayroll PayrollMaster: DoPayroll (1)

  8. PersonnelFile lookup get SS get SS get SS get SS TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS SS SS SS match match match match PayrollMaster: DoPayroll (2)

  9. Time Time Time Time Time getHours getHours getHours getHours getHours PersonnelFile lookup get Time get Time get Time get Time TimeCard TimeCard File getTime getSS getCard Payroll Master DoPayroll SS match PayrollMaster: DoPayroll (3)

  10. Time getHours PersonnelFile lookup PayYourself PayYourself PayYourself Employee Employee Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS getSS match Personnel: Lookup

  11. Time getHours PersonnelFile lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check Check Check Check Check Check getSS match print print print print print print PayrollMaster: DoPayroll (4)

  12. Time getHours Program PersonnelFile main lookup PayYourself Employee TimeCard File getCard Payroll Master TimeCard DoPayroll getTime SS Check getSS match print Payroll Program (2)

  13. SMART and HELPFUL Objects 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!)

  14. 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

  15. Statement of Problem Possible Objects Primary Object Behavior Interface Sample Use Implement Kindergarten OOD

  16. 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

  17. Statement of Problem: Find the value of a portfolio of stocks based on "ticker-tape" information.

  18. Possible Objects: • Portfolio • Holding (a portfolio item) • Value • TickerTape

  19. Primary Object? • not Holding • not Value • Portfolio vs. TickerTape?

  20. Portfolio vs. TickerTape? • Both primary in the sense of independence • But to which object should we send a message to solve our problem?

  21. Is Portfolio the Primary Object? • If so, Portfolio has this behavior: get Value of Portfolio, given TickerTape

  22. 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?

  23. 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

  24. Behavior of Portfolio: • get Value, given TickerTape • constructor, given a file listing of a portfolio

  25. Interface of Portfolio: class Portfolio { Portfolio create() Value getValue(TickerTape t)}

  26. Sample Use: create TickerTape t create Portfolio p to p: get Value v, given t to v: print

  27. Implementing Portfolio: class Portfolio { constructor() { } Value getValue(TickerTape t) { } }

  28. Implementing Portfolio: getValue (1): class Portfolio { Value getValue(TickerTape t) {for each Holding h in Collection c of Holdings { get the Value of h increase the total by v } return total} } • Needed: • STATE: Collection object, c A Portfolio HAS-A collection of holdings • Local Value object, total

  29. 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 h increase the total by v } return total} STATE: Collection c }

  30. Getting The Value of a Holding (1) • This is OOP • Ya want something done, send an object a message to h: getValue

  31. 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

  32. Increasing a Value • This is OOP • Ya want something done, send an object a message to total: increase by ...

  33. Implementing Portfolio: getValue (3): class Portfolio {Value getValue(TickerTape t) { Value total initialized to 0 for 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 } • Remark: Who initializes the Collection?

  34. The Portfolio Constructor (1) class Portfolio {constructor() {create Collection c ... ???}...STATE: Collection c }

  35. 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?

  36. The Portfolio Constructor (2) class Portfolio {constructor(TokenSource src) { create Collection ccreate Holding h, given src while h was created successfully { to c: add h create Holding h, given src }} … STATE: Collection c }

  37. Portfolio Class: class Portfolio { constructor(TokenSource src) { create Collection c create Holding h, given src while h was created successfully { to c: add h create Holding h, given src }} Value getValue(TickerTape t) { Value total initialized to 0 for 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 }

  38. Completed: Portfolio Yet To Complete: TickerTape TokenSource Collection Holding Value Class Summary

  39. Details of Remaining Classes: • TickerTape— constructor • TokenSource — constructor get String • Collection— constructor add ... some way of iterating … • Holding— constructor, given TokenSource get Value given TickerTape • Value— constructor, given an integer value increase, given a Value print

  40. NEXT STEP: Pick a class and follow same procedure… • TickerTape • TokenSource • Collection • Holding • Value

  41. Statement of SubProblem… … skip steps because behavior interface have been determined

  42. Behavior of Holding: • create, given TokenSource • get Value given TickerTape

  43. Interface of Holding: class Holding { constructor(TokenSource src) Value getValue(TickerTape t) }

  44. Sample Use: ... from implementation of Portfolio

  45. Implementing Holding: class Holding { constructor(TokenSource src) { } Value getValue(TickerTape t) } } }

  46. Implementing Holding: getValue class Holding {Value getValue(TickerTape t) {to t: get Value v of stock named by my StockName to v: multiply by my Number return v} } • Remarks: • the object must maintain a StockName and a Number, call them sn and n

  47. Implementing Holding: STATE class Holding {Value getValue(TickerTape t) { to t: get Value v given sn to v: multiply by n return v}STATE: StockName sn Number n }

  48. Implementing Holding: constructor class Holding {constructor(TokenSource src) { create StockName sn, given src create Number n, given src} ... STATE: StockName sn Number n } • Remark: Each class bears the responsibility for doing most of the work to create its own instances

  49. The Holding Class class Holding {constructor(TokenSource src) { create StockName sn, given src create Number n, given src} Value getValue(TickerTape t) { to t: get Value v given sn to v: multiply by n return v} STATE: StockName sn Number n }

  50. Completed: Portfolio Holding Yet To Complete: TickerTape TokenSource Collection Value Number StockName Class Summary

More Related