10/31/2017 - 7:34 PM

Singleton Example

A singleton is both a class and an instantiation in one: there be only one at a given time. This is great for utilities or classes that don't need a state. The object will live through the life span of the program. They are particularly useful for logging, hardware instances, and driver objects.

/*This singleton is a dumbass switch. There is only one, and all we care about is keeping track of whether it is on or off
and what the amperage is on or off. There is no instance, and the key here is there is only one. The Object keyword both defines
a class and instantiates an instance at the same time
object DumbassSwitch{
    private val off = "Off"
    private val on = "On"
    var simplesState: String = off
    var stupidCurrent: Long = 20

    fun flipTheSwitch(){
       if (simplesState==off) on else off
        if(stupidCurrent == 20L) 0 else 20


//Here is where we call the Singleton without having to worry about instantiation. Furthermore, if we call on it from elsewhere
// it will read in its current state
fun main(args: Array<String>){