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
WENDY [Excerpt]
tessaabrams
11
38k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.4k
First, design no harm
axbom
PRO
2
1.2k
Balancing Empowerment & Direction
lara
6
1.2k
AI: The stuff that nobody shows you
jnunemaker
PRO
8
720
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
150
30 Presentation Tips
portentint
PRO
1
330
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.6k
We Have a Design System, Now What?
morganepeng
55
8.2k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
180
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!