72
The State Pattern Thursday, March 18, 2010

State Machine Presentation1 61

  • Upload
    pbalogh

  • View
    254

  • Download
    1

Embed Size (px)

Citation preview

Page 1: State Machine Presentation1 61

The State Pattern

Thursday, March 18, 2010

Page 2: State Machine Presentation1 61

The State PatternAn Appalling Exploration

Thursday, March 18, 2010

Page 3: State Machine Presentation1 61

?

Thursday, March 18, 2010

Page 4: State Machine Presentation1 61

?

Thursday, March 18, 2010

Page 5: State Machine Presentation1 61

Thursday, March 18, 2010

Page 6: State Machine Presentation1 61

Thursday, March 18, 2010

Page 7: State Machine Presentation1 61

Thursday, March 18, 2010

Page 8: State Machine Presentation1 61

Thursday, March 18, 2010

Page 9: State Machine Presentation1 61

Thursday, March 18, 2010

Page 10: State Machine Presentation1 61

Thursday, March 18, 2010

Page 11: State Machine Presentation1 61

Thursday, March 18, 2010

Page 12: State Machine Presentation1 61

Thursday, March 18, 2010

Page 13: State Machine Presentation1 61

Thursday, March 18, 2010

Page 14: State Machine Presentation1 61

Thursday, March 18, 2010

Page 15: State Machine Presentation1 61

Thursday, March 18, 2010

Page 16: State Machine Presentation1 61

Thursday, March 18, 2010

Page 17: State Machine Presentation1 61

:)

Thursday, March 18, 2010

Page 18: State Machine Presentation1 61

Why we need it...

Thursday, March 18, 2010

Page 19: State Machine Presentation1 61

B. F. Skinner(do not do what he did)

Thursday, March 18, 2010

Page 20: State Machine Presentation1 61

Thursday, March 18, 2010

Page 21: State Machine Presentation1 61

Skinner box“Operant

Conditioning Chamber”

Thursday, March 18, 2010

Page 22: State Machine Presentation1 61

Thursday, March 18, 2010

Page 23: State Machine Presentation1 61

FAIL

Thursday, March 18, 2010

Page 24: State Machine Presentation1 61

What you do depends on your state

Thursday, March 18, 2010

Page 25: State Machine Presentation1 61

Blood test

Thursday, March 18, 2010

Page 26: State Machine Presentation1 61

Thursday, March 18, 2010

Page 27: State Machine Presentation1 61

Blood test

NotThing

Thursday, March 18, 2010

Page 28: State Machine Presentation1 61

Thursday, March 18, 2010

Page 29: State Machine Presentation1 61

Blood test

NotThing

Thing

Thursday, March 18, 2010

Page 30: State Machine Presentation1 61

Thursday, March 18, 2010

Page 31: State Machine Presentation1 61

Firstaid

Thursday, March 18, 2010

Page 32: State Machine Presentation1 61

Firstaid

NotThing

Thursday, March 18, 2010

Page 33: State Machine Presentation1 61

Thursday, March 18, 2010

Page 34: State Machine Presentation1 61

Firstaid

NotThing

Thing

Thursday, March 18, 2010

Page 35: State Machine Presentation1 61

Thursday, March 18, 2010

Page 36: State Machine Presentation1 61

Firstaid

NotThing

Thing

Blood test

NotThing

Thing

Thursday, March 18, 2010

Page 37: State Machine Presentation1 61

Firstaid

NotThing

Thing

Blood test

NotThing

Thing

Thursday, March 18, 2010

Page 38: State Machine Presentation1 61

1. Repeatedly using same test

2. Answers to test are predictable and repeated

3. Test concerns one’s own internal matters

Thursday, March 18, 2010

Page 39: State Machine Presentation1 61

Time to get practical.

Thursday, March 18, 2010

Page 40: State Machine Presentation1 61

Thursday, March 18, 2010

Page 41: State Machine Presentation1 61

public class Creature{ public function Creature() { trace(“Ecce Creature”); }}

Thursday, March 18, 2010

Page 42: State Machine Presentation1 61

Thursday, March 18, 2010

Page 43: State Machine Presentation1 61

1. Come to life

Thursday, March 18, 2010

Page 44: State Machine Presentation1 61

1. Come to life2. Talk to us

Thursday, March 18, 2010

Page 45: State Machine Presentation1 61

1. Come to life2. Talk to us3. Move around

Thursday, March 18, 2010

Page 46: State Machine Presentation1 61

public class Creature{

public function Creature() { trace(“Ecce Creature”); }

public function zap() {}

public function talk() {}

public function walk() {} }

Thursday, March 18, 2010

Page 47: State Machine Presentation1 61

public class Creature{

public function Creature() { trace(“Ecce Creature”); }

public function zap() {}

public function talk() {}

public function walk() {} }

Thursday, March 18, 2010

Page 48: State Machine Presentation1 61

Thursday, March 18, 2010

Page 49: State Machine Presentation1 61

Thursday, March 18, 2010

Page 50: State Machine Presentation1 61

Thursday, March 18, 2010

Page 51: State Machine Presentation1 61

... public function implant( b : Brain ) { mybrain = b; }...

Thursday, March 18, 2010

Page 52: State Machine Presentation1 61

Thursday, March 18, 2010

Page 53: State Machine Presentation1 61

public function zap() { // IF DEAD, BECOME ALIVE // IF ALIVE, BECOME DEAD

}

public function walk() {

}

public function talk() { }

Thursday, March 18, 2010

Page 54: State Machine Presentation1 61

public function zap() { // IF DEAD, BECOME ALIVE // IF ALIVE, BECOME DEAD

}

public function talk() { // IF DEAD, DO NOTHING // IF ALIVE, DESCRIBE CANCER CURE

} public function walk() {

}

Thursday, March 18, 2010

Page 55: State Machine Presentation1 61

public function zap() { // IF DEAD, BECOME ALIVE // IF ALIVE, BECOME DEAD

}

public function talk() { // IF DEAD, DO NOTHING // IF ALIVE, DESCRIBE CANCER CURE

}

public function walk() { // IF DEAD, DO NOTHING // IF ALIVE, WALK TO LAB

}

Thursday, March 18, 2010

Page 56: State Machine Presentation1 61

x_x

Thursday, March 18, 2010

Page 57: State Machine Presentation1 61

o_o

Thursday, March 18, 2010

Page 58: State Machine Presentation1 61

0.0

Thursday, March 18, 2010

Page 59: State Machine Presentation1 61

x_x

Thursday, March 18, 2010

Page 60: State Machine Presentation1 61

?

Thursday, March 18, 2010

Page 61: State Machine Presentation1 61

Thursday, March 18, 2010

Page 62: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“Listen to this...”); trace(“the secrets of eternity”);

}}

public function walk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“I’ll go to my lab”); trace(“and cure cancer”);

}}

}

Thursday, March 18, 2010

Page 63: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“Listen to this...”); trace(“the secrets of eternity”);

}}

public function walk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“I’ll go to my lab”); trace(“and cure cancer”);

}}

}

Thursday, March 18, 2010

Page 64: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“Listen to this...”); trace(“the secrets of eternity”);

}}

public function walk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“I’ll go to my lab”); trace(“and cure cancer”);

}}

}

Thursday, March 18, 2010

Page 65: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“Listen to this...”); trace(“the secrets of eternity”);

}}

public function walk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ trace(“I’ll go to my lab”); trace(“and cure cancer”);

}}

}Thursday, March 18, 2010

Page 66: State Machine Presentation1 61

[clip: Igor drops Hans Delbruck’s brain]

Thursday, March 18, 2010

Page 67: State Machine Presentation1 61

Thursday, March 18, 2010

Page 68: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean; private var doIHaveDelbruckBrain : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function implant( b : Brain ) { mybrain = b; if( mybrain == DELBRUCK_BRAIN ) // how?? { doIHaveDelbruckBrain = true; } else { doIHaveDelbruckBrain = false; } }

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ if( doIHaveDelbruckBrain ) {

trace(“Listen to this...”); trace(“the secrets of eternity”); } else { trace(“UNNNNNNNH”); }

}

Thursday, March 18, 2010

Page 69: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean; private var doIHaveDelbruckBrain : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function implant( b : Brain ) { mybrain = b; if( mybrain == DELBRUCK_BRAIN ) // how?? { doIHaveDelbruckBrain = true; } else { doIHaveDelbruckBrain = false; } }

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ if( doIHaveDelbruckBrain ) {

trace(“Listen to this...”); trace(“the secrets of eternity”); } else { trace(“UNNNNNNNH”); }

}

Thursday, March 18, 2010

Page 70: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean; private var doIHaveDelbruckBrain : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function implant( b : Brain ) { mybrain = b; if( mybrain == DELBRUCK_BRAIN ) // how?? { doIHaveDelbruckBrain = true; } else { doIHaveDelbruckBrain = false; } }

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ if( doIHaveDelbruckBrain ) {

trace(“Listen to this...”); trace(“the secrets of eternity”); } else { trace(“UNNNNNNNH”); }

}

Thursday, March 18, 2010

Page 71: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean; private var doIHaveDelbruckBrain : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function implant( b : Brain ) { mybrain = b; if( mybrain == DELBRUCK_BRAIN ) // how?? { doIHaveDelbruckBrain = true; } else { doIHaveDelbruckBrain = false; } }

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ if( doIHaveDelbruckBrain ) {

trace(“Listen to this...”); trace(“the secrets of eternity”); } else { trace(“UNNNNNNNH”); }

}

Thursday, March 18, 2010

Page 72: State Machine Presentation1 61

public class Creature{ private var amIdead : Boolean; private var doIHaveDelbruckBrain : Boolean;

public function Creature() { trace(“Ecce Creature”); amIdead = true;

}

public function implant( b : Brain ) { mybrain = b; if( mybrain == DELBRUCK_BRAIN ) // how?? { doIHaveDelbruckBrain = true; } else { doIHaveDelbruckBrain = false; } }

public function zap() { if( amIdead ) { amIdead = false;

} else

{ amIdead = false;

}}

public function talk() { if( amIdead ) { // DO NOTHING -- I’M DEAD

} else

{ if( doIHaveDelbruckBrain ) {

trace(“Listen to this...”); trace(“the secrets of eternity”); } else { trace(“UNNNNNNNH”); }

}

Thursday, March 18, 2010