Factory Design Pattern in Kotlin

Поділитися
Вставка
  • Опубліковано 15 чер 2022
  • 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-Patterns-and-B...
    Sebastian on Twitter - / sebi_io
    #Kotlin #Factory #Programming
  • Наука та технологія

КОМЕНТАРІ • 42

  • @vitaliiplagov
    @vitaliiplagov 2 роки тому +43

    Really enjoyed the episode! Would like to see more of such episodes on patters. That's useful!

  • @amirhossintaghipour7249
    @amirhossintaghipour7249 2 роки тому +10

    Really enjoyed the episode! Would like to see more of such episodes with coding and discussion over it.

  • @user-om5xe6ih4l
    @user-om5xe6ih4l Рік тому +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.

  • @IncompleteTheory
    @IncompleteTheory Рік тому +7

    Enjoyed this more than I initially expected. Nicely done and edited. Thanks for uploading!

  • @thebatu
    @thebatu 2 роки тому +6

    Great episode! The transition to abstract factory was masterful. More please 😊

  • @m.w.7914
    @m.w.7914 2 роки тому +5

    Nice one - I’d love to see more on design patterns on this channel. Great topic for a series / playlist.

  • @nicoloagnoletti3275
    @nicoloagnoletti3275 Рік тому +3

    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

  • @Poisonedyouth79
    @Poisonedyouth79 2 роки тому +5

    Hope to see more of this

  • @alanesaugarciagutierrez6025
    @alanesaugarciagutierrez6025 2 роки тому +5

    Thanks for this kind of videos, pure gold

  • @ErikHuizinga
    @ErikHuizinga Рік тому +5

    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.
      UA-cam 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 Рік тому

      @@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 Рік тому +5

    Good job guys! Great content!

  • @walkaboutneil
    @walkaboutneil 11 місяців тому

    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.

  • @prasanthmanimaran5158
    @prasanthmanimaran5158 Рік тому +4

    Awesome epsiode as usual

  • @abdallaaltaay4487
    @abdallaaltaay4487 Рік тому +3

    Can you do the builder pattren next pleaaase

  • @ulicqueldromal
    @ulicqueldromal Рік тому +1

    Good Professor Bruegge. I did also very enjoy his lecture.

  • @kolawoleakinde6632
    @kolawoleakinde6632 Рік тому +3

    Thank you for this.

  • @alskaa1
    @alskaa1 Рік тому

    love you guys I really like this episode

  • @rob2theworld
    @rob2theworld Рік тому +2

    Love to see one on the visitors patter

  • @khaled.noordin
    @khaled.noordin 2 роки тому +4

    Nice talk as usual.

  • @st4849
    @st4849 Рік тому

    operator fun invoke(notation: String): Piece

  • @sinarahimi6157
    @sinarahimi6157 Рік тому

    I really enjoyed it. 🌟

  • @er.mudassirkhanmrk1151
    @er.mudassirkhanmrk1151 Рік тому

    Greate episode

  • @mahdihosseinzadeh
    @mahdihosseinzadeh 2 роки тому +6

    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

  • @Das.Kleine.Krokodil
    @Das.Kleine.Krokodil 9 місяців тому

    Thanks

  • @Das.Kleine.Krokodil
    @Das.Kleine.Krokodil 9 місяців тому

    Dialog - excelent way to teach.

  • @teqanyteqany6350
    @teqanyteqany6350 Рік тому

    is there playlist contain all Alexey episode?

  • @donwald3436
    @donwald3436 2 роки тому +2

    Superclasses shouldn't know about subtypes though. Does it being sealed change things? How about it being in the companion?

    • @notkamui9749
      @notkamui9749 2 роки тому +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.

  • @Virus00000000000001
    @Virus00000000000001 Рік тому

    Nice

  • @Eternity0908
    @Eternity0908 2 роки тому +1

    What are the use cases of fake constructors? They also serve as factory methods.

    • @donwald3436
      @donwald3436 2 роки тому +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.

  • @AlanDarkworld
    @AlanDarkworld Рік тому

    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.

  • @rafakuzminski3452
    @rafakuzminski3452 Рік тому +1

    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

  • @user-bz9tz1mf3r
    @user-bz9tz1mf3r Рік тому +1

    Quotes of great men: "Kotlin is easier than Scala, more type safety than Groovy and more fun than Java." 😁

  • @purplehazer417
    @purplehazer417 2 роки тому +4

    хахах, красавчик)

  • @Amejonah
    @Amejonah 2 роки тому

    10:55 JEP 427 goes brr

  • @abhishekoza1240
    @abhishekoza1240 Рік тому

    You are looking like indian actor(saif ali khan) 😁😊😊

  • @djalmasilvajr
    @djalmasilvajr Рік тому

    # 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

  • @youssefmourchid9149
    @youssefmourchid9149 Рік тому

    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 !