This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Worker { | |
/*Async API*/ | |
fun doSomething(listener: Listener, throwError: Boolean) { | |
when (throwError) { | |
true -> { | |
Thread.sleep(3000) | |
listener.onError(Exception("Just a random exception...")) | |
} | |
else -> { | |
Thread.sleep(3000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Custom Base View to support Kotlin's Coroutine according to View's Lifecycle (On Detach, kill Coroutine). | |
* | |
* @param context [Context] | |
* @param attrs [AttributeSet] | |
*/ | |
open class BaseCustomView(context: Context, attrs: AttributeSet) : | |
ConstraintLayout(context, attrs) { | |
private val tag by lazy { this::class.java.canonicalName ?: "BaseCustomView" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GenericBodyExecutor { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
println(executeBodyOrReturnNull<Int> { GenericData("0").data.toInt() }) // Result -> 0 | |
println(executeBodyOrReturnNull<Int> { GenericData("Hello").data.toInt() }) // Result -> null | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Our interface that has only one method [Delegation.print]. | |
* Who inherit [Delegation] might have different implementation. | |
* So we are declaring [Delegation.print] without any kind of parameter. | |
*/ | |
interface Delegation { | |
fun print() | |
} | |
/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Kotlin's Coroutine extension functions to switch from one context to another one. | |
* Supports [Dispatchers.IO], [Dispatchers.Main], [Dispatchers.Default], [Dispatchers.Unconfined]. | |
*/ | |
suspend inline fun CoroutineScope.toIO(crossinline scope: suspend CoroutineScope.() -> Unit) { | |
withContext(Dispatchers.IO) { scope.invoke(this) } | |
} | |
suspend inline fun CoroutineScope.toMain(crossinline scope: suspend CoroutineScope.() -> Unit) { | |
withContext(Dispatchers.Main) { scope.invoke(this) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FlyweightExample { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
val raceCars = arrayOf( | |
RaceCarClient("Midget"), | |
RaceCarClient("Midget"), | |
RaceCarClient("Midget"), | |
RaceCarClient("Sprint"), | |
RaceCarClient("Sprint"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SafeExecutorExample { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
withSafeExecutor<Any> { | |
Either.Right(Any()) } | |
/* | |
Test by throwing exception. | |
*/ | |
withSafeExecutor<Any> { throw Exception("Test") }.either( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompositeDesignPattern { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
/*Create primary products for main catalog*/ | |
val mJeanProduct: CatalogComponent = Product("M: Jeans 32", 65.00) | |
val mTShirtProduct: CatalogComponent = Product("M: T Shirt 38", 45.00) | |
/*Create a composite product catalog and add female products to it*/ | |
val newCatalog: CatalogComponent = ProductCatalog("Female Products") | |
val fJeans: CatalogComponent = Product("F: Jeans 32", 65.00) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BridgeDesignPattern { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
// Send new text message | |
val textMessageSender: MessageSender = TextMessageSender() | |
val textMessage: Message = TextMessage(textMessageSender) | |
textMessage.send() | |
// Send new email message |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DecoratorDesignPattern { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
val rose = RoseBouquet() | |
println("Without customization = ${rose.description}, ${rose.cost()}") | |
val roseWrapper = PaperWrapper(rose) | |
println("With customization = ${roseWrapper.description}, ${roseWrapper.cost()}") | |
val orchid = OrchidBouquet() |