Тёмный

Factory Design Pattern in Kotlin 

Kotlin by JetBrains
Подписаться 76 тыс.
Просмотров 27 тыс.
50% 1

Alexey Soshin, the author of “Kotlin Design Patterns and Best Practices”, sits down with Sebastian to discuss the factory design pattern - a creational design pattern popularized by the “Gang of Four” book “Design Patterns: Elements of Reusable Object-Oriented Software”. Alexey guides Sebastian through the ins and outs of the pattern, and they discuss benefits, naming conventions, and more.
Buy Kotlin Design Patterns and Best Practices - kotl.in/Design...
Sebastian on Twitter - / sebi_io
#Kotlin #Factory #Programming

Опубликовано:

 

15 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 44   
@vitaliiplagov
@vitaliiplagov 2 года назад
Really enjoyed the episode! Would like to see more of such episodes on patters. That's useful!
@IncompleteTheory
@IncompleteTheory 2 года назад
Enjoyed this more than I initially expected. Nicely done and edited. Thanks for uploading!
@amirhossintaghipour7249
@amirhossintaghipour7249 2 года назад
Really enjoyed the episode! Would like to see more of such episodes with coding and discussion over it.
@ДмитрийБречко-м9с
@ДмитрийБречко-м9с 2 года назад
Nice to listen to the experienced people. Just have read book about design pattern and i would be very happy to see more episodes about them. Thanks guys.
@thebatu
@thebatu 2 года назад
Great episode! The transition to abstract factory was masterful. More please 😊
@Poisonedyouth79
@Poisonedyouth79 2 года назад
Hope to see more of this
@m.w.7914
@m.w.7914 2 года назад
Nice one - I’d love to see more on design patterns on this channel. Great topic for a series / playlist.
@ErikHuizinga
@ErikHuizinga 2 года назад
Not sure how others think about this (so please comment what you think), but I think there is more to the Factory Method (or Factory Function / Factory Lambda) pattern than just the use some method/function/lambda to map one type to another. I think what's part of the classical pattern, which is missing here, is that you declare an abstract (possibly default) function in some super entity, e.g. an interface. E.g. `interface GameLoader { fun loadGame(notation: List): List }`. That really lets you vary the `GameLoader` implementation at the call site, without the call site changing. Just having some top-level function mapping strings to pieces isn't really the classical Factory Function pattern, right? That's like calling a type alias equivalent to a type defined from a class or interface. Simply because the signature matches of a random different function that maps strings to pieces, it doesn't mean that it is a Factory Function pattern, because you can plug it in at the call site. There might be different use cases for mapping strings to pieces that do not load a game. Likewise, defining it in the companion object of the source or target type, or just as an extension, is equally flawed: there is no way to vary implementations of the function without actually using the classical pattern, where some interface/super class declared the Factory Function. So: consider wrapping the function in some interface an implementing different variants as implementations. Yes, I know: that is introducing classic OOP boilerplate. But it _does_ provide the type safety that the implementor _must_ inherit from `GameLoader` and that you can only use `GameLoader` instances throughout your code. That is completely omitted in this video. What do you think about that? I do agree that the Static Factory Function (mentioned in the video) is a simpler version of the Factory Function, but they're not the same.
@alexeysoshin8442
@alexeysoshin8442 2 года назад
Thank you for the thoughtful comment. RU-vid comments section is not the best place to discuss code concepts, but I'll do my best. I agree with your points, this implementation does have its tradeoffs compared to the classical implementation that you describe. You may need that extra level of abstraction if you design a library, and especially if you plan that users of the library may need to extend it. But it would be very hard to encompass all those nuances in a video, without confusing anyone. And I do think that a more pragmatic approach for Design Patterns is beneficial for most of the Kotlin applications.
@utagaiby
@utagaiby 2 года назад
@@alexeysoshin8442 Have to agree with Eric. The example you've provided in the video and in the book is a factory (or simple factory) pattern, not factory method. Factory method implies that you can override construction logic somewhere.
@nicoloagnoletti3275
@nicoloagnoletti3275 2 года назад
The extension function is fantastic to implement the static factory pattern when you don't want the Piece class to have knowledge of more complex classes or you want to separate the application module into "layers" and you want a piece out of a class that doesn't belong to ita layer
@alanesaugarciagutierrez6025
@alanesaugarciagutierrez6025 2 года назад
Thanks for this kind of videos, pure gold
@ArthurKhazbs
@ArthurKhazbs Месяц назад
Interesting take on if-else vs when. I only use if-else when it fits in a one-line expression, otherwise I go the when route.
@walkaboutneil
@walkaboutneil Год назад
very nice intro to thinkings on factory methods. I very much liked the iteration to the final method. Just enough on items to point out nicities of Kotlin. Thanks.
@ulicqueldromal
@ulicqueldromal 2 года назад
Good Professor Bruegge. I did also very enjoy his lecture.
@abdallaaltaay4487
@abdallaaltaay4487 2 года назад
Can you do the builder pattren next pleaaase
@nicoloagnoletti3275
@nicoloagnoletti3275 2 года назад
Good job guys! Great content!
@rob2theworld
@rob2theworld 2 года назад
Love to see one on the visitors patter
@prasanthmanimaran5158
@prasanthmanimaran5158 2 года назад
Awesome epsiode as usual
@rafakuzminski3452
@rafakuzminski3452 2 года назад
I bought your book, but I am little confused. You describe "builder" pattern, but it isn't a real builder pattern. It is just simple "assembler", without any concrete builders or engineer. There wasn't event any mention, that it is simplified version that is called "builder" by java community. If anyone plans to buy this - it is not bad, but it will be better to just stick with original Design Patterns, and translate those patterns to Kotlin yourself
@kolawoleakinde6632
@kolawoleakinde6632 2 года назад
Thank you for this.
@st4849
@st4849 2 года назад
operator fun invoke(notation: String): Piece
@mahdihosseinzadeh
@mahdihosseinzadeh 2 года назад
Shouldn't it be drop(1) instead of drop(0)? And, do you think the following produces the same result as the first two statements of the method? val pieceType = name.first() val position = name.substring(1)
@3bdoelnaggar
@3bdoelnaggar 2 года назад
ya you are right drop calls substring and substring takes beginIndex inclusive
@Talaria.School
@Talaria.School 2 года назад
Nice talk as usual.
@easybusinessou
@easybusinessou Год назад
is there playlist contain all Alexey episode?
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Год назад
Dialog - excelent way to teach.
@ArthurKhazbs
@ArthurKhazbs Месяц назад
"We all are Chinese rooms." Thank you, now I know it's OK to feel this way sometimes! :)
@donwald3436
@donwald3436 2 года назад
Superclasses shouldn't know about subtypes though. Does it being sealed change things? How about it being in the companion?
@notkamui9749
@notkamui9749 2 года назад
depends ; sometimes you really dont want to overengineer things. In the case of sealed classes/interfaces, you know no other implementations will exist, and those will generally be extremely simple POJOs/data classes. With that in mind, this allows for generally unsafe patterns to become safe, such as pattern matching / safe when casts, or what you see in this video.
@alskaa1
@alskaa1 2 года назад
love you guys I really like this episode
@Eternity0908
@Eternity0908 2 года назад
What are the use cases of fake constructors? They also serve as factory methods.
@donwald3436
@donwald3436 2 года назад
By fake constructor, do you mean operator fun invoke on companions? I like that style when there's a "copy constructor" (the real constructor, declaring properties) and one "convenience constructor" (the factory method) because the intent and IDE hints are cleaner. For multiple factory methods I like to name them.
@sinaAngelo
@sinaAngelo 2 года назад
I really enjoyed it. 🌟
@er.mudassirkhanmrk1151
@er.mudassirkhanmrk1151 2 года назад
Greate episode
@AlanDarkworld
@AlanDarkworld 2 года назад
The example is a "factory" of some sort, but it misses the mark on the topic a bit. To me, a factory is an interface my code receives from the caller, and it abstracts away the creation of an object. I don't know the concrete class, but I do know the interface it implements, and the factory builds it for me. In the chess example, it would mean that the individual implementations of "Piece" were unknown within "fromNotation", and it would receive the notation as well as an interface that maps a piece letter to a corresponding object that implements Piece.
@Das.Kleine.Krokodil
@Das.Kleine.Krokodil Год назад
Thanks
@youssefmourchid9149
@youssefmourchid9149 2 года назад
You could talk also about some other structural patterns (abstract factory or factory method ..), also i'am not a fan of using the static factory method in the Piece itself, we should separate the creation of the system from using it, imagine another team wants only the piece, why they will take also the the factory Nice video thank you, waiting for the next one !
@КонстантинМаксимов-п3о
Quotes of great men: "Kotlin is easier than Scala, more type safety than Groovy and more fun than Java." 😁
@Virus00000000000001
@Virus00000000000001 2 года назад
Nice
@purplehazer417
@purplehazer417 2 года назад
хахах, красавчик)
@Amejonah
@Amejonah 2 года назад
10:55 JEP 427 goes brr
@djalmasilvajr
@djalmasilvajr 2 года назад
# I wrote a version with sealed interface and data class val notation = listOf("pa3","qc5") fun main() { val gameState = notation.map { Piece.fromNotation(it) } println(gameState) } sealed interface Piece { val position: String companion object { fun fromNotation(piece: String): Piece { val pieceType = piece[0] val position = piece.substring(1) return when (pieceType) { 'p' -> Pawn(position) 'q' -> Queen(position) else -> error("Unknown piece!") } } } } data class Pawn(override val position:String): Piece data class Queen(override val position:String): Piece
@abhishekoza1240
@abhishekoza1240 Год назад
You are looking like indian actor(saif ali khan) 😁😊😊
Далее
Design Patterns in Kotlin Episode #2 | Kotlin Tutorial
25:27
Context Receivers Are Coming to Kotlin!
31:44
Просмотров 37 тыс.
The State Design Pattern in Python Explained
19:14
Просмотров 76 тыс.
Every Kind of Class in Kotlin
10:44
Просмотров 12 тыс.
NEW OPERATOR in Kotlin (and true open-ended ranges)
13:47
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Kotlin Evergreen Tips: Destructuring Declarations
7:25