Lecture08_Handouts1slide

Lecture08_Handouts1slide - TDDB84 Design Patterns TDDB84...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
DDB84 Design Patterns TDDB84 Design Patterns Lecture 08 State, Interpreter, Facade eter Bunus 2008 Peter Bunus Department of Computer and Information Science Linköping University, Sweden etbu@ida.liu.se Copyright © P petbu@ida.liu.se
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
TDDB84 Design Patterns Slide 2 State
Background image of page 2
State – Non Software Example TDDB84 Design Patterns Slide 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Gumball Vending Machine TDDB84 Design Patterns Slide 4
Background image of page 4
Writting Code ublic class umballMachine { 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 GumballMachine( int count) { this.count = count; if (count > 0) { state = NO_QUARTER; } } TDDB84 Design Patterns Slide 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ublic void nsertQuarter() { 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 "); } else if (state == SOLD) { System.out.println(" Please wait, we're already giving you a gumball "); } } TDDB84 Design Patterns Slide 6
Background image of page 6
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, you haven't inserted a quarter yet "); } } TDDB84 Design Patterns Slide 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Testing the GumBall Machine public class GumballMachineTestDrive { public static void main(String[] args) { umballMachine gumballMachine = new GumballMachine(5); g( ) ; System.out.println(gumballMachine); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); System.out.println(gumballMachine); umballMachine.insertQuarter(); gumballMachine.insertQuarter(); gumballMachine.ejectQuarter(); gumballMachine.turnCrank(); System.out.println(gumballMachine); gumballMachine.insertQuarter(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); umballMachine insertQuarter(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); gumballMachine.insertQuarter(); gumballMachine.turnCrank(); TDDB84 Design Patterns Slide 8 System.out.println(gumballMachine); } }
Background image of page 8
You expected it. Isn’t it? Joe, we need to reward somehow customers that are constantly uying gums from our machines. 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
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
What we need to do public class GumballMachine { You have to add a new WINNER tate here final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; state here int state = SOLD_OUT; int count = 0; ...but then, you’d have to add a new conditional in every single method to handle the WINNER state, that’s a lot of code to public void insertQuarter(){ //insert quarter code here } ublic void jectQuarter(){ modify p j Q (){ //insert quarter code here } public void turnCrank(){
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 49

Lecture08_Handouts1slide - TDDB84 Design Patterns TDDB84...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online