dbacinski /Design-Patterns-In-Kotlin

Design Patterns implemented in Kotlin

★ 5,989⑂ 685👁 193⑃ 34● 11▤ 0
KotlinGPL-3.0Created 2016-09-26
-1
日增长
5,989
stars
685
forks
193
watchers
34
prs
11
issues
0
releases

README

Design Patterns In Kotlin

 :warning: New article about testing: Unit Testing with Mockito 2

Project maintained by @dbacinski (Dariusz Baciński)

Inspired by Design-Patterns-In-Swift by @nsmeme (Oktawian Chojnacki)

 Table of Contents

 Behavioral Patterns
	 Observer / Listener
	 Strategy
	 Command
	 State
	 Chain of Responsibility
	 Visitor
	 Mediator
	 Memento
 Creational Patterns
	 Builder / Assembler
	 Factory Method
	 Singleton
	 Abstract Factory
 Structural Patterns
	 Adapter
	 Decorator
	 Facade
	 Protection Proxy
	 Composite

Behavioral
==========

In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

Source: wikipedia.org

Observer / Listener
--------

The observer pattern is used to allow an object to publish changes to its state.
Other objects subscribe to be immediately notified of any changes.

 Example

kotlin
interface TextChangedListener {

    fun onTextChanged(oldText: String, newText: String)
}

class PrintingTextChangedListener : TextChangedListener {
    
    private var text = ""
    
    override fun onTextChanged(oldText: String, newText: String) {
        text = "Text is changed: $oldText - $newText"
    }
}

class TextView {

    val listeners = mutableListOf()

    var text: String by Delegates.observable("") { _, old, new -
        listeners.forEach { it.onTextChanged(old, new) }
    }
}


 Usage

kotlin
val textView = TextView().apply {
    listeners.add(PrintingTextChangedListener())
}

with(textView) {
    text = "Lorem ipsum"
    text = "dolor sit amet"
}


 Output


Text is changed  - Lorem ipsum
Text is changed Lorem ipsum - dolor sit amet


Strategy
-----------

The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time.

 Example

kotlin
class Printer(private val stringFormatterStrategy: (String) - String) {

    fun printString(string: String) {
        println(stringFormatterStrategy(string))
    }
}

val lowerCaseFormatter: (String) - String = { it.toLowerCase() }
val upperCaseFormatter = { it: String - it.toUpperCase() }


 Usage

kotlin
val inputString = "LOREM ipsum DOLOR sit amet"

val lowerCasePrinter = Printer(lowerCaseFormatter)
lowerCasePrinter.printString(inputString)

val upperCasePrinter = Printer(upperCaseFormatter)
upperCasePrinter.printString(inputString)

val prefixPrinter = Printer { "Prefix: $it" }
prefixPrinter.printString(inputString)


 Output


lorem ipsum dolor sit amet
LOREM IPSUM DOLOR SIT AMET
Prefix: LOREM ipsum DOLOR sit amet


Command
-------

The command pattern is used to express a request, including the call to be made and all of its required parameters, in a command object. The command may then be executed immediately or held for later use.

 Example:

kotlin
interface OrderCommand {
    fun execute()
}

class Orde

Top issues

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON