I want to learn design patterns with Kotlin. If th...
# getting-started
d
I want to learn design patterns with Kotlin. If there is any good learning source that can help me, it will be helpful if you can share it with me. I have come across a few blogs, but they are not informative enough.
d
I have already come across: 1. GitHub - dbacinski/Design-Patterns-In-Kotlin: Design Patterns implemented in Kotlin 2. Design patterns with Kotlin Part 1 In the GitHub source, the examples have much better examples compared to the medium article. But the GitHub source does not explain it well (for me, I am new to this)... is there something that has good real usecase examples like in that GitHub repo with more explanations? And if I want to practice, how should I proceed?
p
This is also a good resource for working with design patterns in Kotlin https://www.packtpub.com/product/kotlin-design-patterns-and-best-practices-second-edition/9781801815727
👍 1
s
I started writing about design patterns in kotlin, literally yesterday. I felt the same challenge that I wanted to see real examples; which is what my focus will in my articles. Here is the first article https://medium.com/@humbleshuttler/design-patterns-in-kotlin-demystifying-singleton-pattern-7c0373d77fc7 PS: I have just started my journey on tech writing. Feedback is highly appreciated.
K 2
d
Code to an interface rather than to an implementation.
Can anyone explain this? Examples will also be helpful.
s
A simple example would be to write functions that accept
List
(an interface) instead of
ArrayList
(a specific class that implements that interface). That makes your code more reusable and versatile, as it will be able to work with all types of lists.
Copy code
fun printValues(items: List<String>) { ... } // good
fun printValues(items: ArrayList<String>) {... } // less good
d
Interfaces are mainly used for delegating class functions... Please bare with me if I am using the wrong terms (I am just learning).
c
Please don't send your messages to the channel. There are already people answering you, and you're spamming everyone else.
💯 3
3
s
“Delegating” might not be the exact right term, but I think you have the right idea 👍 interfaces generally contain abstract (unimplemented) functions, and the implementation is provided by one or more concrete subclasses. Programming to an interface is considered good practice because it means your code can work with any one of those concrete subclasses, not just the specific one you chose.
K 1
👍 1
t
It is that sense all about function signatures and how easy they are to use. if you have a function that takes a String and produces an Int, the signature is String -> Int. In this case you have the option to do ArrayList -> Unit, or List -> Unit. The last function will work for any implementation of List (e.g. ArrayList and LinkedList). In most cases the implementation of your function does not really case about a specific implementation of the list (if you just want to loop over it for example, you dont care if it is an ArrayList or a LinkedList). Therefore you can make your function more generic by using the interface instead of a specific implementation.
d
In most class diagrams I see that there is a get-method for each property of the class. But in Kotlin we can do
someClass.someProperty
to access it. Why need those get-methods in the class diagrams?
s
Probably just for interoperability with Java. When you access a Kotlin property from Java you will see a public
get
and
set
method, and the underlying field will be private.
👍 1
c
Class diagrams are not that standardized. Most people who use them have a Java background, where they are necessary, so they tend to write them anyway. You don't really have to bother too much with them, as you said in Kotlin they're not necessary
👍 1
t
Yeah, officially there is UML, but in practice most people keep them practical
c
@Ties I meant, UML doesn't go into the details of whether setters and getters are displayed, etc
UML also has "computed properties" marked as
/foo
, which in Kotlin are properties without a backing field (or with a backing field different enough that you can consider it's something else)
In general, don't take UML as "a golden truth of what your code should be like", it's more of a "here's the general idea, you can adapt the details to your situation", it's almost always fine to switch around what is a property or a method
💯 1
Btw same thing with design patterns, it's not because they exist that you should use them! In practice, especially in Kotlin with all the syntax we have (
object
,
by
, DSLs…) it's rare to use a "proper design pattern"
After all, "design pattern" means "the way most of us agree upon to do something the language cannot do" (if the language could do it, it would be a language feature, not a design pattern)
d
OK
164 Views