Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Design Patterns in Kotlin
Search
Sohel Shaikh
July 06, 2024
77
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Design Patterns in Kotlin
Presented at KSUG, Surat
Sohel Shaikh
July 06, 2024
More Decks by Sohel Shaikh
See All by Sohel Shaikh
Supercharge your Android Apps with Gemini API
thesohelshaikh
1
130
Featured
See All Featured
30 Presentation Tips
portentint
PRO
1
330
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
440
The browser strikes back
jonoalderson
0
1.3k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
220
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Designing for humans not robots
tammielis
254
26k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
Believing is Seeing
oripsolob
1
160
Why Our Code Smells
bkeepers
PRO
340
58k
Fireside Chat
paigeccino
42
4k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
420
Transcript
Design Patterns in Kotlin Sohel Shaikh Lead Android Engineer @
Skylark Drones
- 4+ YOE with Android - Lead Android Engineer @
Skylark Drones - I 💜 Kotlin! - @thesohelshaikh on Socials Hey, I am Sohel
A Pattern is a solution to a problem in a
context.
A Pattern is a solution to a problem in a
context. A recurring situation
A Pattern is a solution to a problem in a
context. A recurring situation Goal in context
A Pattern is a solution to a problem in a
context. A recurring situation Goal in context The design
History
What is it? Instead of code reuse, with patterns you
get experience reuse.
Why bother? - Shared vocabularies - Discussions at a higher
level - Tried and tested solutions - Easy to reuse
Singleton Pattern
object Keyword object Logger { fun log(line: String) { println(line)
} }
Create Room DB instance val db = Room.databaseBuilder( applicationContext, AppDatabase-:class.java,
"database-name" ).build()
Singleton with Params class AppDatabase { companion object { private
var INSTANCE: YTDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE -: Room.databaseBuilder( context.applicationContext, AppDatabase-:class.java, "app_database" ).build().apply { INSTANCE = this } } } }
Singleton with Params class AppDatabase { companion object { private
var INSTANCE: YTDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE -: Room.databaseBuilder( context.applicationContext, AppDatabase-:class.java, "app_database" ).build().apply { INSTANCE = this } } } }
Singleton with Params class AppDatabase { companion object { private
var INSTANCE: YTDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE -: Room.databaseBuilder( context.applicationContext, AppDatabase-:class.java, "app_database" ).build().apply { INSTANCE = this } } } }
Singleton with Params (Thread safe) class AppDatabase { companion object
{ @Volatile private var INSTANCE: YTDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE -: synchronized(this) { val instance = Room.databaseBuilder( context.applicationContext, AppDatabase-:class.java, "app_database" ).build().apply { INSTANCE = instance } } } } }
Singleton with Params (Thread safe) class AppDatabase { companion object
{ @Volatile private var INSTANCE: YTDatabase? = null fun getDatabase(context: Context): AppDatabase { return INSTANCE -: synchronized(this) { val instance = Room.databaseBuilder( context.applicationContext, AppDatabase-:class.java, "app_database" ).build().apply { INSTANCE = instance } } } } }
Singleton with Params (Thread safe) class MyApplication : Application() {
val database by lazy { AppDatabase.getDatabase(this) } }
An Anti-Pattern tells you how to go from a problem
to BAD Solution.
Singleton Cons - Creates Global state - Hard to debug
- Hard to test - Hides a complex design flaw - Thread safety handling
Observer Pattern aka Pub-Sub Pattern aka Event Bus
java.util
None
class KotlinNewsLetter { val newestArticleObservers = mutableListOf<KotlinDeveloper>() var newestArticleUrl: String
by Delegates.observable("") { _, _, newValue -> newestArticleObservers.forEach { it.read(newValue) } } } Kotlin Delegates
class KotlinNewsLetter { val newestArticleObservers = mutableListOf<KotlinDeveloper>() var newestArticleUrl: String
by Delegates.observable("") { _, _, newValue -> newestArticleObservers.forEach { it.read(newValue) } } } Kotlin Delegates
class KotlinNewsLetter { val newestArticleObservers = mutableListOf<KotlinDeveloper>() var newestArticleUrl: String
by Delegates.observable("") { _, _, newValue -> newestArticleObservers.forEach { it.read(newValue) } } } class KotlinDeveloper { fun read(article: String) { println("$this Reading $article") } } Kotlin Delegates
val newsLetter = KotlinNewsLetter() val developerAlpha = KotlinDeveloper() val developerBeta
= KotlinDeveloper() newsLetter.newestArticleObservers.add(developerAlpha) newsLetter.newestArticleObservers.add(developerBeta) newsLetter.newestArticleUrl = "www.kotlin.com" newsLetter.newestArticleUrl = "www.java-is-bad.com"
None
Factory Pattern enum class ExportFormat{ Csv, Pdf } interface Exporter
{ fun export(): File } class CsvExporter: Exporter { override fun export(): File { --. } } class PdfExporter: Exporter { override fun export(): File { --. } }
Factory Pattern class ExporterFactory { companion object { fun create(format:
ExportFormat): Exporter { return when(format) { ExportFormat.Csv -> CsvExporter() ExportFormat.Pdf -> PdfExporter() } } } }
Factory Pattern val exporter = ExporterFactory.create(ExportFormat.Csv) val file = exporter.export()
Factory Pattern with different constructor parameters?
Sealed Classes
None
Memento Pattern Use the Memento Pattern when you need to
be able to return an object to one of its previous states; for instance, if your user requests an “undo.”
UML class diagram of Memento pattern
Design Principle Identify the aspects of your application that vary
and separate them from what stays the same.
Abstraction of Video broadcasting functionality
Design Principle Program to an interface, not an implementation.
Abstraction of event tracking mechanism
Be Cautious! - Another tool in the toolbox - Hard
to refactor later - Focus on the problem
Modern Recommendations
Unlocking the economic potential of the skies We’re Hiring! Skylark
Drones
What are your questions? Too shy? Ping me @thesohelshaikh
Thank you!