Last active
November 22, 2017 12:08
-
-
Save timmolderez/e6b40f80fa6e2603abb977d6c03f1332 to your computer and use it in GitHub Desktop.
Design patterns exercise (observer + strategy)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat | |
import java.util.Date | |
object Clock { | |
val TIME_24 = 0 | |
val TIME_US = 1 | |
val TIME_UNIX = 2 | |
def main(args: Array[String]): Unit = { | |
val c = new Clock | |
c.setDisplayType(TIME_24) | |
new Thread(c).start() | |
// After a few seconds, switch to a different display type | |
Thread.sleep(3000) | |
c.setDisplayType(TIME_UNIX) | |
} | |
} | |
class Clock extends Runnable { | |
private var displayType = Clock.TIME_24 | |
def setDisplayType(dt: Int): Unit = { | |
displayType = dt | |
} | |
override def run(): Unit = { | |
while (true) { | |
var time = "" | |
if (displayType == Clock.TIME_24) time = new SimpleDateFormat("HH:mm:ss").format(new Date) | |
else if (displayType == Clock.TIME_US) time = new SimpleDateFormat("hh:mm:ss a").format(new Date) | |
else if (displayType == Clock.TIME_UNIX) time = String.valueOf(System.currentTimeMillis / 1000L) | |
System.out.println(time) | |
Thread.sleep(1000) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment