Skip to content

Instantly share code, notes, and snippets.

@alifgiant
Last active July 20, 2021 08:02
Show Gist options
  • Save alifgiant/b073e7145254eec89fe631f2b523f096 to your computer and use it in GitHub Desktop.
Save alifgiant/b073e7145254eec89fe631f2b523f096 to your computer and use it in GitHub Desktop.
MVC example with scheduler
/**
 * Money dan Coffee adalah Model
 * ShopController adalah Si OB "Controller"
 * ConsumerView adalah Si Bos "View"
 */
class CustomerView {   
   var coffee = EmptyCoffeeCup()

   val shopper = ShopController() // si OB
   val person = PersonController() // otak si Bos
   
   fun onRefillClick() {
      val money = person.getMoney(15000)
      coffee = shopper.makeCoffee(money)
   }

   fun onDrinkCoffeeClick() {
      person.consume(coffee) // coffee.volume -= 5 (minum dikit2)
   }
   
   val coffeEmptyCheckScheduler = Scheduler(every = 10_MINUTE) {
      if (coffee.isEmpty()){
         onRefillClick()
      }
   }
   
   init {
      coffeEmptyCheckScheduler.run()
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment