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
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
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
370
Docker and Python
trallard
47
3.9k
Accessibility Awareness
sabderemane
1
140
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Side Projects
sachag
455
43k
My Coaching Mixtape
mlcsv
0
150
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
780
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
How GitHub (no longer) Works
holman
316
150k
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!