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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Sohel Shaikh
July 06, 2024
78
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
Crafting Experiences
bethany
1
210
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
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
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
Six Lessons from altMBA
skipperchong
29
4.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Accessibility Awareness
sabderemane
1
150
Paper Plane (Part 1)
katiecoart
PRO
0
9.6k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
Faster Mobile Websites
deanohume
310
32k
Agile that works and the tools we love
rasmusluckow
331
22k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
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!