tddb84 design patterns lecture 09 state prototype visitor
play

TDDB84 Design Patterns Lecture 09 State, Prototype, Visitor, - PowerPoint PPT Presentation

TDDB84 Design Patterns Lecture 09 State, Prototype, Visitor, Flyweight Peter Bunus Department of Computer and Information Science Linkping University, Sweden peter.bunus@liu.se State TDDB84 Design Patterns Slide 2 State Non Software


  1. TDDB84 Design Patterns Lecture 09 State, Prototype, Visitor, Flyweight Peter Bunus Department of Computer and Information Science Linköping University, Sweden peter.bunus@liu.se

  2. State TDDB84 Design Patterns Slide 2

  3. State – Non Software Example TDDB84 Design Patterns Slide 3

  4. The Gumball Vending Machine Has Quarter Inserts quarter ejects quarter turns crank No Quarter Out of gumballs gumballs>0 gumballs=0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 4

  5. Writting Code public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; Has Quarter final static int HAS_QUARTER = 2; final static int SOLD = 3; Inserts quarter ejects quarter int state = SOLD_OUT; turns crank int count = 0; No Quarter Out of gumballs public GumballMachine(int count) { this.count = count; if (count > 0) { gumballs>0 state = NO_QUARTER; gumballs=0 Gumball } sold } dispense gumballs TDDB84 Design Patterns Slide 5

  6. Writting the Code public void insertQuarter() { if (state == HAS_QUARTER) { System.out.println("You can't insert another quarter"); } else if (state == NO_QUARTER) { state = HAS_QUARTER; System.out.println("You inserted a quarter"); } else if (state == SOLD_OUT) { System.out.println("You can't insert a quarter, the machine is sold out"); Has Quarter } else if (state == SOLD) { System.out.println("Please wait, we're already giving you a gumball"); Inserts quarter ejects quarter } } turns crank No Quarter Out of gumballs gumballs>0 gumballs=0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 6

  7. Writting the Code public void ejectQuarter() { if (state == HAS_QUARTER) { System.out.println("Quarter returned"); state = NO_QUARTER; } else if (state == NO_QUARTER) { System.out.println("You haven't inserted a quarter"); } else if (state == SOLD) { System.out.println("Sorry, you already turned the crank"); } else if (state == SOLD_OUT) { System.out.println("You can't eject, Has Quarter you haven't inserted a quarter yet"); Inserts quarter ejects quarter } } turns crank No Quarter Out of gumballs gumballs>0 gumballs=0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 7

  8. Testing the GumBall Machine public class GumballMachineTestDrive { public static void main(String[] args) { GumballMachine gumballMachine = new GumballMachine(5); System.out.println(gumballMachine); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); System.out.println(gumballMachine); gumballMachine.insertQuarter(); gumballMachine.ejectQuarter(); gumballMachine.turnCrank(); System.out.println(gumballMachine); gumballMachine.insertQuarter(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); System.out.println(gumballMachine); } TDDB84 Design Patterns } Slide 8

  9. You expected it. Isn’t it? Joe, we need to reward somehow customers that are constantly buying gums from our machines. Let ’ s implement reward system that gives you a free gum if you already purchased 10 gums. TDDB84 Design Patterns Slide 9

  10. What we need to do public class GumballMachine { You have to add a new WINNER state here final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; ...but then, you ’ d have to add a int state = SOLD_OUT; new conditional in every single int count = 0; method to handle the WINNER state, that ’ s a lot of code to modify public void insertQuarter(){ //insert quarter code here } public void ejectQuarter(){ turnCrank() will get especially //insert quarter code here messy, because you ’ d have to } add code to check to see public void turnCrank(){ whether you ’ ve got a winner //insert quarter code here and then switch to either the } WINNER state or SOLD state public void dispense(){ //insert quarter code here } } TDDB84 Design Patterns Slide 10

  11. Redesigning the Gumball Machine «interface» State +insertQuarter() +ejectQuarter() +turnCrank() +dispense() SoldState SoldOutState NoQuarterState HasQuarterState +insertQuarter() +insertQuarter() +insertQuarter() +insertQuarter() +ejectQuarter() +ejectQuarter() +ejectQuarter() +ejectQuarter() Has Quarter +turnCrank() +turnCrank() +turnCrank() +turnCrank() +dispense() +dispense() +dispense() +dispense() Inserts quarter ejects quarter turns crank No Quarter Out of gumballs gumballs>0 gumballs=0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 11

  12. NoQuarterState Go to HasQuarterState Has Quarter NoQuarterState Inserts quarter ejects quarter +insertQuarter() +ejectQuarter() turns crank +turnCrank() No Quarter +dispense() Out of gumballs Tell the customer: “ You haven ’ t inserted a quarter ” gumballs>0 gumballs=0 Gumball sold Tell the customer: “ You dispense gumballs turned, but there's no quarter ” Tell the customer: “ You need to pay first ” TDDB84 Design Patterns Slide 12

  13. NoQuarterState public class NoQuarterState implements State { GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("You inserted a quarter"); gumballMachine.setState(gumballMachine.getHasQuarterState()); } public void ejectQuarter() { System.out.println("You haven't inserted a quarter"); } public void turnCrank() { System.out.println("You turned, but there's no quarter"); } public void dispense() { System.out.println("You need to pay first"); } public String toString() { return "waiting for quarter"; } } TDDB84 Design Patterns Slide 13

  14. The State Gumball Machine public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; int state = SOLD_OUT; int count = 0; public class GumballMachine { State soldOutState; State noQuarterState; State hasQuarterState; State soldState; State state = soldOutState; int count = 0; TDDB84 Design Patterns Slide 14

  15. The State Gumball Machine public class GumballMachine { public void insertQuarter() { state.insertQuarter(); State soldOutState; } State noQuarterState; State hasQuarterState; public void ejectQuarter() { State soldState; state.ejectQuarter(); } State state = soldOutState; int count = 0; public void turnCrank() { state.turnCrank(); public GumballMachine(int numberGumballs) { state.dispense(); soldOutState = new SoldOutState(this); } noQuarterState = new NoQuarterState(this); hasQuarterState = new HasQuarterState(this); void setState(State state) { soldState = new SoldState(this); this.state = state; } this.count = numberGumballs; if (numberGumballs > 0) { void releaseBall() { System.out.println("A gumball state = noQuarterState; comes } rolling out the slot..."); } if (count != 0) { count = count - 1; } TDDB84 Design Patterns } Slide 15

  16. The State Design Pattern The Context is the class that can have a number The State interface defines common of internal states. interface for all concrete states; the states all implement the same interface, so they are interchangeable Whenever the request() is made on the Context it is delegated to the state Each concrete class provides its own implementation for a request. In this way, to handle when the Context changes state, its behavior changes as well TDDB84 Design Patterns Slide 16

  17. How about my extra gumball? turns crank winner=0 && gumballs>1 Winner dispense 2 gumballs Has Quarter gumballs=0 Inserts quarter ejects quarter gumballs>0 turns crank No Quarter Out of gumballs gumballs>0 gumballs=0 Gumball sold dispense gumballs TDDB84 Design Patterns Slide 17

  18. The Winner State public class WinnerState implements State { GumballMachine gumballMachine; public WinnerState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } public void ejectQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } public void turnCrank() { System.out.println("Turning again doesn't get you another gumball!"); } public void dispense() { System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter"); gumballMachine.releaseBall(); if (gumballMachine.getCount() == 0) { gumballMachine.setState(gumballMachine.getSoldOutState()); } else { gumballMachine.releaseBall(); if (gumballMachine.getCount() > 0) { gumballMachine.setState(gumballMachine.getNoQuarterState()); } else { System.out.println("Oops, out of gumballs!"); gumballMachine.setState(gumballMachine.getSoldOutState()); } TDDB84 Design Patterns } Slide 18 }

Recommend


More recommend