Basic class design
This presentation is the property of its rightful owner.
Sponsored Links
1 / 89

Basic Class Design PowerPoint PPT Presentation


  • 87 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Basic class design

Basic Class Design


Example payroll

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

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.


Payroll classes

PayrollMaster

Employee

PersonnelFile

TimeCard

TimeCardFile

Check

SS#

Hours

Payroll Classes


Payroll objects

Time

getHours

Program

PersonnelFile

main

lookup

PayYourself

Employee

TimeCard File

getCard

Payroll Master

TimeCard

DoPayroll

getTime

SS

Check

getSS

match

print

Payroll Objects


Payroll program 1

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)


Payrollmaster dopayroll 1

PersonnelFile

lookup

TimeCard

TimeCard

TimeCard

TimeCard File

getTime

getTime

getTime

getSS

getSS

getSS

getCard

Payroll Master

DoPayroll

PayrollMaster: DoPayroll (1)


Payrollmaster dopayroll 2

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)


Payrollmaster dopayroll 3

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)


Personnel lookup

Time

getHours

PersonnelFile

lookup

PayYourself

PayYourself

PayYourself

Employee

Employee

Employee

TimeCard File

getCard

Payroll Master

TimeCard

DoPayroll

getTime

SS

getSS

match

Personnel: Lookup


Payrollmaster dopayroll 4

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)


Payroll program 2

Time

getHours

Program

PersonnelFile

main

lookup

PayYourself

Employee

TimeCard File

getCard

Payroll Master

TimeCard

DoPayroll

getTime

SS

Check

getSS

match

print

Payroll Program (2)


A peculiar employee design

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


Oop and o o design

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


Kindergarten ood

Statement of Problem

Possible Objects

Primary Object

Behavior

Interface

Sample Use

Implement

Kindergarten OOD


Kindergarten ood1

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


Statement of problem

Statement of Problem:

Find the value of a portfolio of stocksbased on "ticker-tape" information.


Possible objects

Possible Objects:

  • Portfolio

  • Holding (a portfolio item)

  • Value

  • TickerTape


Primary object

Primary Object?

  • not Holding

  • not Value

  • Portfolio vs. TickerTape?


Portfolio vs tickertape

Portfolio vs. TickerTape?

  • Both primary in the sense of independence

  • But to which object should we send a messageto solve our problem?


Is portfolio the primary object

Is Portfolio the Primary Object?

  • If so, Portfolio has this behavior:

    get Value of Portfolio, given TickerTape


Is tickertape the primary object

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?


Responsibility driven design

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


Behavior of portfolio

Behavior of Portfolio:

  • get Value, given TickerTape

  • constructor, given a file listing of a portfolio


Interface of portfolio

Interface of Portfolio:

class Portfolio {Portfolio create()Value getValue(TickerTape t)}


Sample use

Sample Use:

create TickerTape tcreate Portfolio pto p: get Value v, given tto v: print


Implementing portfolio

Implementing Portfolio:

class Portfolio {

constructor() {

}

Value getValue(TickerTape t) {

}

}


Implementing portfolio getvalue 1

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


Implementing portfolio getvalue 2

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

}


Getting the value of a holding 1

Getting The Value of a Holding (1)

  • This is OOP

  • Ya want something done, send an object a message

    to h: getValue


Getting the value of a holding 2

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


  • Increasing a value

    Increasing a Value

    • This is OOP

    • Ya want something done, send an object a message

      to total: increase by ...


    Implementing portfolio getvalue 3

    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 tto total: increase by v}return total}

    STATE: Collection c

    }

    • Remark: Who initializes the Collection?


    The portfolio constructor 1

    The Portfolio Constructor (1)

    class Portfolio {constructor() {create Collection c... ???}...STATE: Collection c

    }


    Constructor questions and answers

    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?


    The portfolio constructor 2

    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

    }


    Portfolio class

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

    }


    Class summary

    Completed:

    Portfolio

    Yet To Complete:

    TickerTape

    TokenSource

    Collection

    Holding

    Value

    Class Summary


    Details of remaining classes

    Details of Remaining Classes:

    • TickerTape— constructor

    • TokenSource — constructor get String

    • Collection— constructoradd... some way of iterating …

    • Holding— constructor, given TokenSourceget Value given TickerTape

    • Value— constructor, given an integer valueincrease, given a Valueprint


    Next step pick a class and follow same procedure

    NEXT STEP: Pick a class and follow same procedure…

    • TickerTape

    • TokenSource

    • Collection

    • Holding

    • Value


    Statement of subproblem

    Statement of SubProblem…

    … skip steps because

    behavior

    interface

    have been determined


    Behavior of holding

    Behavior of Holding:

    • create, given TokenSource

    • get Value given TickerTape


    Interface of holding

    Interface of Holding:

    class Holding {constructor(TokenSource src)Value getValue(TickerTape t)

    }


    Sample use1

    Sample Use:

    ... from implementation of Portfolio


    Implementing holding

    Implementing Holding:

    class Holding {

    constructor(TokenSource src) {

    }

    Value getValue(TickerTape t) }

    }

    }


    Implementing holding getvalue

    Implementing Holding: getValue

    class 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


    Implementing holding state

    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

    }


    Implementing holding constructor

    Implementing Holding: constructor

    class 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


    The holding class

    The Holding Class

    class 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

    }


    Class summary1

    Completed:

    Portfolio

    Holding

    Yet To Complete:

    TickerTape

    TokenSource

    Collection

    Value

    Number

    StockName

    Class Summary


    Details of remaining classes i

    Details of Remaining Classes (I):

    NEW!

    • TickerTape— constructor

      • get Value given a StockName

    • TokenSource — constructor

      • get String

    • Collection— constructor

      • add

      • ... some way of iterating …


    Details of remaining classes ii

    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!


    Next step pick a class and follow procedure

    NEXT STEP: Pick a class and follow procedure....

    • TickerTape

    • TokenSource

    • Collection

    • Value

    • Number

    • StockName


    Statement of subproblem1

    Statement of SubProblem:

    • ... skip steps because behavior, interface has been determined


    Behavior of tickertape

    Behavior of TickerTape:

    • Create

    • get Value given StockName


    Interface of tickertape

    Interface of TickerTape:

    class TickerTape {constructor()TickerTape getValue(StockName sn)

    }


    Sample use2

    Sample Use:

    ... from implementation of Holding


    Implementing tickertape

    Implementing TickerTape

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

    }


    Implementing tickertape getvalue

    Implementing TickerTape: getValue

    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}

    }

    Remark: need a Collection in my state


    Implementing tickertape state

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

    }


    Implementing tickertape constructor

    Implementing TickerTape: constructor

    class TickerTape {constructor(TokenSource src) {create Collection ccreate StockQuote s given srcwhile not failure {to c: add screate StockQuote s given src}}...

    STATE:Collection c

    }


    The tickertape class

    The TickerTape Class

    class TickerTape {constructor(TokenSource src) {create Collection ccreate StockQuote s given srcwhile not failure {to c: add screate StockQuote s given src}}


    The tickertape class continued

    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}


    Class summary2

    Completed:

    Portfolio

    Holding

    TickerTape

    Yet To Complete:

    TokenSource

    Collection

    Value

    Number

    StockName

    StockQuote

    Class Summary


    Details of remaining classes i1

    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


    Details of remaining classes ii1

    Details of Remaining Classes (II):

    NEW!

    • Number— constructor

    • StockName— constructor

    • StockQuote— constructor, given a TokenSource

      • match a StockName

      • return the Value of a stock


    Next step pick a class and follow procedure1

    NEXT STEP: Pick a class and follow procedure....

    • TickerTape

    • TokenSource

    • Collection

    • Value

    • Number

    • StockName

    • StockQuote


    Statement of subproblem2

    Statement of SubProblem:

    ... skip steps because behavior, interface has been determined


    Behavior of stockquote

    Behavior of StockQuote:

    • create, given a TokenSource

    • get Value

    • is match? given a StockName


    Interface of stockquote

    Interface of StockQuote:

    class StockQuote {constructor(TokenSource src)Value getValue()boolean isMatch(StockName sn)

    }


    Sample use3

    Sample Use:

    • ... from implementation of TickerTape


    Implementing stockquote

    Implementing StockQuote

    class StockQuote {constructor(TokenSource src) {

    }

    StockQuote getValue() {

    }

    boolean isMatch(StockName sn) {

    }

    }


    Implementing stockquote i

    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

    }


    Implementing stockquote ii

    Implementing StockQuote (II)

    class StockQuote {constructor(TokenSource src) {create StockName sn, given srccreate Value v, given src}...STATE:Value vStockName sn

    }


    The stockquote class

    The StockQuote Class

    class 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

    }


    Class summary3

    Completed:

    Portfolio

    Holding

    TickerTape

    StockQuote

    Yet To Complete:

    TokenSource

    Collection

    Value

    Number

    StockName

    Class Summary


    Details of remaining classes i2

    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


    Details of remaining classes ii2

    Details of Remaining Classes (II):

    • Number— constructor

    • StockName— constructor

      • is Match? given another StockName

    NEW!


    Next step pick a class and follow procedure2

    NEXT STEP: Pick a class and follow procedure....

    • TickerTape

    • TokenSource

    • Collection

    • Value

    • Number

    • StockName


    Statement of subproblem3

    Statement of SubProblem:

    • ... skip steps because behavior, interface has been determined


    Behavior of value

    Behavior of Value:

    • create, given a TokenSource

    • increase, given a Value

    • multiply, given a Number

    • print


    Interface of value

    Interface of Value:

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

    }


    Sample use4

    Sample Use:

    ... from implementation of TickerTape


    Implementing value

    Implementing Value

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

    }


    Implementing value constructor

    Implementing Value: constructor

    class Value {constructor(TokenSource src) {to src: get String integerPartto src: get String fractionto fraction: contains / ?if noto src: put back String fraction// just use integerPartelse// use integerPart and fraction}...

    }


    Implementing value other methods

    Implementing Value: other methods

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

    }


    Class summary4

    Completed:

    Portfolio

    Holding

    TickerTape

    StockQuote

    Value (partially)

    Yet To Complete:

    TokenSource

    Collection

    Number

    StockName

    Class Summary


    Details of remaining classes1

    Details of Remaining Classes:

    NEW!

    • 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


    Next step

    NEXT STEP:

    And so on …

    (I'm tired now— aren't you?)


  • Login