Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of.

Similar presentations


Presentation on theme: "Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of."— Presentation transcript:

1

2 Basic Class Design

3 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

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

5 Payroll Classes PayrollMaster Employee PersonnelFile TimeCard TimeCardFile Check SS# Hours

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

7 TimeCard File getCard Payroll Master DoPayroll Payroll Master DoPayroll Personnel File lookup TimeCard File getCard Payroll Program (1) Program main Payroll Master DoPayroll Personnel File lookup Personnel File lookup TimeCard File getCard

8 TimeCard File getCard Payroll Master DoPayroll Personnel File lookup TimeCard getSS getTime TimeCard getSS getTime TimeCard getSS getTime PayrollMaster: DoPayroll (1)

9 TimeCard getSS getTime TimeCard File getCard Payroll Master DoPayroll Personnel File lookup get SS SS match SS match get SS SS match get SS PayrollMaster: DoPayroll (2) get SS SS match

10 get Time Time getHours get Time Time getHours PayrollMaster: DoPayroll (3) TimeCard getSS getTime TimeCard File getCard Payroll Master DoPayroll Personnel File lookup SS match get Time Time getHours Time getHours Time getHours

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

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

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

14 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

15 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

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

17 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

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

19 Possible Objects: Portfolio Holding (a portfolio item) Value TickerTape

20 Primary Object? not Holding not Value Portfolio vs. TickerTape?

21 Both primary in the sense of independence But to which object should we send a message to solve our problem?

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

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

24 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

25 Behavior of Portfolio: get Value, given TickerTape constructor, given a file listing of a portfolio

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

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

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

29 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

30 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 }

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

32 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

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

34 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?

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

36 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?

37 The Portfolio Constructor (2) 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 } } … STATE: Collection c }

38 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 }

39 Class Summary Completed: –Portfolio Yet To Complete: –TickerTape –TokenSource –Collection –Holding –Value

40 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

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

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

43 Behavior of Holding: create, given TokenSource get Value given TickerTape

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

45 Sample Use:... from implementation of Portfolio

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

47 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

48 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 }

49 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

50 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 }

51 Class Summary Completed: –Portfolio –Holding Yet To Complete: –TickerTape –TokenSource –Collection –Value –Number –StockName

52 Details of Remaining Classes (I): TickerTape— constructor –get Value given a StockName TokenSource — constructor –get String Collection— constructor –add –... some way of iterating … NEW!

53 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!

54 NEXT STEP: Pick a class and follow procedure.... TickerTape TokenSource Collection Value Number StockName

55 Statement of SubProblem:... skip steps because behavior, interface has been determined

56 Behavior of TickerTape: Create get Value given StockName

57 Interface of TickerTape: class TickerTape { constructor() TickerTape getValue(StockName sn) }

58 Sample Use:... from implementation of Holding

59 Implementing TickerTape class TickerTape { constructor() { } Value getValue(StockName sn) { } }

60 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 v return v } } error } }

61 Implementing TickerTape: 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 v return v } } error } STATE:Collection c }

62 Implementing TickerTape: constructor class TickerTape { constructor(TokenSource src) { create Collection c create StockQuote s given src while not failure { to c: add s create StockQuote s given src } }... STATE:Collection c }

63 The TickerTape Class class TickerTape { constructor(TokenSource src) { create Collection c create StockQuote s given src while not failure { to c: add s create StockQuote s given src } }

64 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 v return v } } error } STATE:Collection c }

65 Class Summary Completed: –Portfolio –Holding –TickerTape Yet To Complete: –TokenSource –Collection –Value –Number –StockName –StockQuote

66 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

67 Details of Remaining Classes (II): Number— constructor StockName— constructor StockQuote— constructor, given a TokenSource –match a StockName –return the Value of a stock NEW!

68 NEXT STEP: Pick a class and follow procedure.... TickerTape TokenSource Collection Value Number StockName StockQuote

69 Statement of SubProblem:... skip steps because behavior, interface has been determined

70 Behavior of StockQuote: create, given a TokenSource get Value is match? given a StockName

71 Interface of StockQuote: class StockQuote { constructor(TokenSource src) Value getValue() boolean isMatch(StockName sn) }

72 Sample Use:... from implementation of TickerTape

73 Implementing StockQuote class StockQuote { constructor(TokenSource src) { } StockQuote getValue() { } boolean isMatch(StockName sn) { }

74 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 v StockName sn }

75 Implementing StockQuote (II) class StockQuote { constructor(TokenSource src) { create StockName sn, given src create Value v, given src }... STATE:Value v StockName sn }

76 The StockQuote Class class StockQuote { constructor(TokenSource src) { create StockName sn, given src create 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 v StockName sn }

77 Class Summary Completed: –Portfolio –Holding –TickerTape –StockQuote Yet To Complete: –TokenSource –Collection –Value –Number –StockName

78 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

79 Details of Remaining Classes (II): Number— constructor StockName— constructor –is Match? given another StockName NEW!

80 NEXT STEP: Pick a class and follow procedure.... TickerTape TokenSource Collection Value Number StockName

81 Statement of SubProblem:... skip steps because behavior, interface has been determined

82 Behavior of Value: create, given a TokenSource increase, given a Value multiply, given a Number print

83 Interface of Value: class Value { Value create(TokenSource src) void increase(Value v) void multiply(Number n) void print() }

84 Sample Use:... from implementation of TickerTape

85 Implementing Value class Value { Value create(TokenSource src) { } void increase(Value v) { } void multiply(Number n) { } void print() { } }

86 Implementing Value: constructor class Value { constructor(TokenSource src) { to src: get String integerPart to src: get String fraction to fraction: contains / ? if no to src: put back String fraction // just use integerPart else // use integerPart and fraction }... }

87 Implementing Value: other methods class Value { Value create(TokenSource src) {... } void increase(Value v) { // defer } void multiply(Number n) { // defer } void print() { // defer } STATE: ??? }

88 Class Summary Completed: –Portfolio –Holding –TickerTape –StockQuote –Value (partially) Yet To Complete: –TokenSource –Collection –Number –StockName

89 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!

90 NEXT STEP: And so on … (I'm tired now— aren't you?)


Download ppt "Basic Class Design Example: Payroll Purpose: Picture the object-creation, message-passing interplay of objects in an executing program. Statement of."

Similar presentations


Ads by Google