CloudCoders / Design-Patterns

Project for learning and discuss about design patterns
16 stars 1 forks source link

Added Observer pattern #37

Closed Cotel closed 7 years ago

Cotel commented 7 years ago

Solves #13

It is ready to be merged, BUT I think the example can be improved.

By using an anonymous implementation for the Observer :

interface Observer<in T> {
  fun onValueChange(newValue: T, oldValue: T)
}

class Shop {
  private val observer = object : Observer<Int> {
    override fun onValueChange(newValue: Int, oldValue: Int) = when {
      newValue > oldValue -> println(...)
      else -> println(...)
    }
  }

  private var currentCustomers by Delegates.observable(0) { _, old, new ->
    observer.onValueChange(new, old)
  }

  // ...
}

Or even removing the Observer interface :

class Shop {
  private var currentCustomers by Delegates.observable(0) { _, old, new ->
    onValueChange(new, old)
  }

  private fun onValueChange(newValue: Int, oldValue: Int) = // ...

  // ...
}

Although this last one may not respect the SOLID rules 🤔

What do you think? Should we change it before merging?

tonilopezmr commented 7 years ago

oh fuck, don't care