dbacinski / design-patterns-in-kotlin Goto Github PK
View Code? Open in Web Editor NEWDesign Patterns implemented in Kotlin
License: GNU General Public License v3.0
Design Patterns implemented in Kotlin
License: GNU General Public License v3.0
The following code utilizes the sealed class keyword in Kotlin. I think it can be an improvement for the example in the Factory section. Will be glad to hear your opinion:
interface Currency {
val code: String
}
class Euro(override val code: String = "EUR") : Currency
class UnitedStatesDollar(override val code: String = "USD") : Currency
class UnitedKingdomPound(override val code: String = "GBP") : Currency
sealed class Country
data class UnitedStates(val name: String = "US"): Country()
data class Spain(val name: String = "Spain"): Country()
data class UK(val name: String = "UK"): Country()
data class Greece(val name: String = "Greece"): Country()
class CurrencyFactory {
fun currencyForCountry(country: Country): Currency {
return when (country) {
is Spain, is Greece -> Euro()
is UnitedStates -> UnitedStatesDollar()
is UK -> UnitedKingdomPound()
}
}
}
`class PrintingTextChangedListener : TextChangedListener {
private var text = ""
override fun onTextChanged(oldText: String, newText: String) {
text = "Text is changed: $oldText -> $newText"
println(text) // here
}
}`
I suppose that the usage example of Observer/Listener should be this:
val textView = TextView().apply {
listeners.add(PrintingTextChangedListener())
}
with(textView) {
text = "Lorem ipsum"
text = "dolor sit amet"
}
It is right in the pattern detail though.
Why should not we use an object instead of a class?
object CurrencyFactory {
fun currencyForCountry(country: Country): Currency? {
return when (country) {
Country.Spain, Country.Greece -> Euro()
Country.UnitedStates -> UnitedStatesDollar()
else -> null
}
}
}
Hello,
I have seen some nice repository pattern implementations in Java but cannot find one in Kotlin.
Thanks!
https://github.com/dbacinski/Design-Patterns-In-Kotlin#state
val isAuthorized: Boolean
val userLogin: String
mb better put them after state field?
I am planning to write an integration test. But how to mock UserRepository
.
Usually, when implementing the observer pattern, the subject contains a list of observers, so it allows whatever class that is interested in its events to listen to them. In your (however beautiful) example there can be only one observer. So my suggestion is to use a list of observers in the subject, and a method named addObserver(observer: Any)
to allow adding observers.
As it is, the example compiles and has the same effect as if it didn't have : CoffeeMachine by coffeeMachine
so it is really only demonstrating composition.
//Decorator:
class EnhancedCoffeeMachine(val coffeeMachine: CoffeeMachine) : CoffeeMachine by coffeeMachine {
//overriding behaviour
override fun makeLargeCoffee() = println("Enhanced: Making large coffee")
//extending behaviour
fun makeCoffeeWithMilk() {
println("Enhanced: Making coffee with milk")
coffeeMachine.makeSmallCoffee()
println("Enhanced: Adding milk")
}
fun makeDoubleLargeCoffee() {
println("Enhanced: Making double large coffee")
coffeeMachine.makeLargeCoffee()
coffeeMachine.makeLargeCoffee()
}
}
Usage:
val normalMachine = NormalCoffeeMachine()
val enhancedMachine = EnhancedCoffeeMachine(normalMachine)
// non-overridden behaviour
enhancedMachine.makeSmallCoffee()
// overridden behaviour
enhancedMachine.makeLargeCoffee()
//extended behaviour (not sure why there's need for two examples of this)
enhancedMachine.makeCoffeeWithMilk()
enhancedMachine.makeDoubleLargeCoffee()
Will you create a sample for Null Object pattern?
I think that is interesting how you create the builder pattern for learning Kotlin DSL, but why just don't use apply? Maybe add the apply alternative should be great because it is available for all objects out of the box.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.