Summary
This was a long chapter, but we’ve also learned a lot. We finished covering all the classical design patterns, including 11 behavioral ones. In Kotlin, functions can be passed to other functions, returned from functions, and assigned to variables. That’s what the higher-order functions and functions as first-class citizens concepts are all about. If your class is all about behavior, it often makes sense to replace it with a function. This is one of the big advantages when compared to Java and leads to less classes and less complexity. This concept helped us implement the Strategy and Command design patterns.
We learned that the Iterator design pattern is yet another operator in the language. Sealed classes make the when
statements exhaustive and we used them to implement the State design pattern.
We also looked at the Interpreter design pattern and learned that lambda with a receiver allows clearer syntax in your DSLs. Another keyword, lateinit
, tells...