Skip to content

Instantly share code, notes, and snippets.

@holyjak
Created November 18, 2013 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/7532967 to your computer and use it in GitHub Desktop.
Save holyjak/7532967 to your computer and use it in GitHub Desktop.
[Surfacing Hidden Design: A Better Alternative To Interrelated Mutable Fields](http://wp.me/p3ui6n-1q) - 1) Explicit and defect-preventing expression of the states and transitions - full code
// P.S.: Excuse the underscores ...
class Person (val id: Int) {
// Simplified primitive version, assuming the information when should the evolution
// happen is handled somewhere else
def evolveHealth(): Unit = {
if (!_infected)
if (randomGenerator.randomBelow(101) <= config.transmissibilityPct)
this._infected = true
if (_infected && !_sick && !_immune)
this._sick = true
if (_sick)
if (randomGenerator.randomBelow(101) <= config.mortalityPct)
this._dead = true
else
this._unDead = true // did not die but still sick
if (_unDead)
this._immune = true
this._sick = true
if (_immune)
this._immune = false
this._infected = false
}
private var _unDead = false
private var _infected = false
private var _sick = false
private var _immune = false
private var _dead = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment