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
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
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.9k
Believing is Seeing
oripsolob
1
170
WCS-LA-2024
lcolladotor
0
680
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
870
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.9k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
310
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Making Projects Easy
brettharned
120
6.7k
Designing for humans not robots
tammielis
254
26k
Color Theory Basics | Prateek | Gurzu
gurzu
0
390
Faster Mobile Websites
deanohume
310
32k
We Are The Robots
honzajavorek
0
270
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!