GithubHelp home page GithubHelp logo

dbacinski / design-patterns-in-kotlin Goto Github PK

View Code? Open in Web Editor NEW
5.8K 5.8K 690.0 269 KB

Design Patterns implemented in Kotlin

License: GNU General Public License v3.0

Kotlin 100.00%
design design-patterns kotlin kotlin-android

design-patterns-in-kotlin's People

Contributors

dbacinski avatar deniskrr avatar harshmittal2810 avatar jolkdarr avatar kenfehling avatar lightway82 avatar mfrankowicz avatar mgrzeszczak avatar mibrahimdev avatar ming13 avatar net-geek avatar ntedgi avatar pabl0rg avatar stepango avatar yozh1que avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

design-patterns-in-kotlin's Issues

Improvement For The Factory Example

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()
}
}
}

Factory and object instead of class

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
        }
    }
}

Use a list of observers instead of only one

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.

Decorator doesn't demonstrate overriding behaviour, only extending it

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()

Builder and apply

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.