Applying the Strategy Pattern in Kotlin

แชร์
ฝัง
  • เผยแพร่เมื่อ 1 ก.พ. 2025

ความคิดเห็น • 57

  • @KonusSequencer
    @KonusSequencer 5 หลายเดือนก่อน +18

    Your channel is absolutely brilliant! The delievery, the pace, lack of flashy distractions, and very clear explanations. Thank you from the bottom of my heart!

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Oh wow, thank you so much! I'm really glad you're enjoying it!

  • @luisfelipecoronadotorres962
    @luisfelipecoronadotorres962 5 หลายเดือนก่อน +15

    Ok, now I need - Design Patterns in Kotlin: An illustrated guide!! 📖📖

    • @typealias
      @typealias  5 หลายเดือนก่อน +2

      Haha, I don't know if I'm ready to start another book just yet, Luis! 😅 But it's a fun idea! 😁

  • @CommanderSteps
    @CommanderSteps 5 หลายเดือนก่อน +11

    Your content is extremely valuable for teaching my trainee. 👌👍

    • @typealias
      @typealias  5 หลายเดือนก่อน

      That's so great to hear, Stefan! 🎉

  • @LifeLessonsLearned8587
    @LifeLessonsLearned8587 5 หลายเดือนก่อน +8

    Series on design pattern and amalgamation of kotlin is what I needed the most. I have never understood the statement "encapsulate what varies" but now I would always remember "extract what differs".
    Keep up the good work Dave!
    Thanks for the video

    • @typealias
      @typealias  5 หลายเดือนก่อน +3

      That's fantastic, Vivek! Sounds like lots of Kotlin developers have been interested in the topic, so I'm glad these videos are resonating well. I'll keep at it!

  • @guillermoduquesanchez
    @guillermoduquesanchez 5 หลายเดือนก่อน +3

    Wow, you are awesome! That's a very smooth way of teaching.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thank you so much, Guillermo!

  • @IlianLepilov-e5z
    @IlianLepilov-e5z 4 หลายเดือนก่อน +1

    I really like how this code looks after your refactoring. But I see one potential drawback. Before to create a concrete implementation of FormField you just needed to call the constructor and the validator is built-in. Now you need to make sure that you pass an appropriate validator for your field, and from my opinion this is error-prone, especially in the production. We might solve this problem by introducing a Factory or something similar combined with your refactoring but, again, we will get more code. What do you think?

    • @typealias
      @typealias  4 หลายเดือนก่อน +2

      Hey Ilian, thanks for reaching out! I think I understand what you're getting at - the earlier approach had an actual type (e.g., EmailValidator vs. UsernameValidator) whereas by the end, they're all just the same type - (String) -> Boolean, and we'd have to be careful to use the right validator on the right kind of value.
      If we want the compiler to enforce that, I'm thinking we could still keep the latter approach, but might consider using simple value classes for the types, and generics for the validators. Maybe something like this:
      ```
      interface Value { val value: String }
      @JvmInline value class Email(override val value: String) : Value
      typealias Validator = (T) -> Boolean
      val emailValidator: Validator = { it.value.contains("@") && it.value.contains(".") }
      class FormField(val name: String, val value: T, private val validator: Validator) {
      fun isValid() = validator(value)
      }
      fun Validator.optional(): Validator = { it.value.isEmpty() || this(it) }
      ```
      I only included the value class for Email in the code here, but same idea for the Username and Password. This wouldn't add too much code, and having strong types for those values would usually be a good idea anyway!

    • @IlianLepilov-e5z
      @IlianLepilov-e5z 4 หลายเดือนก่อน

      @@typealias Thank you VERY much for the detailed answer.

  • @guyguy467
    @guyguy467 5 หลายเดือนก่อน +1

    Thank you. I like how you start simple and evolve the code from java like to Kotlin specific. Design patterns are always a good subject.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thanks, Guy! Yes, there were enough viewers asking me to cover design patterns that I couldn't say no. 😅

  • @mohammad-rezaei2018
    @mohammad-rezaei2018 5 หลายเดือนก่อน +1

    Thank you for this great content! It's an excellent start for a design patterns series. The video on the Strategy Pattern in Kotlin is informative and well-presented. Combining theory with practical Kotlin examples, this approach sets a strong foundation for an educational series on design patterns. Keep up the good work

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thanks so much, Mohammad! I'll keep at it! 👍

  • @RamazanAbdulaev
    @RamazanAbdulaev 5 หลายเดือนก่อน +1

    You explain very well, I'm very glad I found you!

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thanks so much, Ramazan! I'm glad you're here!

  • @mankinPT
    @mankinPT 5 หลายเดือนก่อน +1

    Would be cool if you could show what shortcuts you are using during the presentation (I think it's called Presentation Assistant).
    Curious about the multi select + search...

    • @typealias
      @typealias  5 หลายเดือนก่อน +4

      I've customized my keyboard shortcuts quite a lot, so I'm sure I'd be lost if I started typing on someone else's installation of IntelliJ. 😅 But yes, the multi-caret feature is really useful! I most often use "Add Selection for Next Occurrence", which I think is Alt+J by default. You can also click and drag while holding down Alt if you just want a caret on each row. And there's also Alt+Shift+Click to add a caret wherever you click.

  • @jackli1924
    @jackli1924 5 หลายเดือนก่อน

    amazing and informative as usaul! Thanks so much Dave. You always rock
    me!

  • @falahhassan7671
    @falahhassan7671 5 หลายเดือนก่อน +1

    Your content is very valuable; keep going!

  • @dk6024
    @dk6024 5 หลายเดือนก่อน +11

    In Kotlin, I find myself making only data classes and functions and deriving interfaces only to object declarations. This seems to be a happy place; data and functions, OOP is a very minor part of it.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      That's a great approach! I do love that Kotlin doesn't box us in to one paradigm or another.

    • @stanislavzemlyakov5442
      @stanislavzemlyakov5442 5 หลายเดือนก่อน

      I stopped crying on this video, when I read your comment. Thanks God, we are not the only ones who uses Kotlin the same way.

  • @ChrisAthanas
    @ChrisAthanas 5 หลายเดือนก่อน +5

    Useful breakdown

  • @QuantuMGriD
    @QuantuMGriD 5 หลายเดือนก่อน

    Great to see my favourite topic comming often! I am getting goose bumps when I see professionals like you being so good at explaining concepts in a very neat, simple and understandable way. Thank you very much Sir😊.

    • @typealias
      @typealias  5 หลายเดือนก่อน +1

      You are most welcome! More videos for this topic coming soon! 👍

  • @vaibhavpatil8297
    @vaibhavpatil8297 5 หลายเดือนก่อน

    This was a great video with high quality content, keep posting, the channel should boom soon.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thank you, Vaibhav! I'm having a great time creating these videos, and I'm really glad others are finding them helpful!

  • @AntonArhipov
    @AntonArhipov 5 หลายเดือนก่อน +3

    Great content!

    • @typealias
      @typealias  5 หลายเดือนก่อน +1

      Thanks so much, Anton! I appreciate that!

    • @AntonArhipov
      @AntonArhipov 5 หลายเดือนก่อน +2

      @@typealias I'm very curious about what comes next ;) The visitor pattern is another one that transforms tremendously with the flexibility of the language.

    • @typealias
      @typealias  5 หลายเดือนก่อน +2

      Yes! I had actually started working on the video for the visitor pattern before this one, but it was possible to take the visitor pattern in quite a few different directions, with a variety of different data types that it might operate on. So I decided to put it on the back burner until I had a little longer to mull it over. 🙂 Definitely looking forward to that one, though!

  • @ErikBongers
    @ErikBongers 5 หลายเดือนก่อน +6

    The Strategy pattern is one of those patterns that automatically emerge if you factor out, as you said it, only the stuff that is different.
    In general, the most useful patterns are the ones you constantly use or implement without realising it.
    Now, about monads...

    • @typealias
      @typealias  5 หลายเดือนก่อน +2

      Haha, yeah! I might get into more FP approaches at some point.

  • @LightDante
    @LightDante 2 หลายเดือนก่อน +1

    3:47 What the heck happened? Can you explain how to do it

  • @MootazLtaief
    @MootazLtaief 5 หลายเดือนก่อน

    Amazing :) keep up the great work

  • @osisuper98
    @osisuper98 5 หลายเดือนก่อน

    Loved it!

  • @nehaljain8193
    @nehaljain8193 5 หลายเดือนก่อน

    Hi Dave, your content is awesome, totally loved it, Can you please make a video on MVVM architecture pattern? Thanks a ton :)

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thanks so much, Nahal! I'll add MVVM to the list of ideas for future videos!

  • @benchakalaka
    @benchakalaka 5 หลายเดือนก่อน +1

    Your video is just a one place to go - if you want to be a good software developer! Thank you!

  • @valcron-1000
    @valcron-1000 5 หลายเดือนก่อน +1

    I'm currently writing C# in my day job and I wish I could end up with code similar to your final Kotlin version yet C# does not support "top level functions".
    Another thing to keep in mind is that in your class based version you could instantiate the same validator even tough it makes no sense since as you said they have no state at all. You most likely want to use `object` rather than `class` if you want to keep the class based approach.

  • @hendibrahim875
    @hendibrahim875 5 หลายเดือนก่อน

    Looking forward to seeing more videos

  • @esionecneics3459
    @esionecneics3459 3 หลายเดือนก่อน

    Thanks for the great explanation. Given the classic strategy pattern, that would mean you still have to implement the optional variants e.g. OptionalUsernameValidator, right?
    class OptionalUsernameValidator: Validator {
    override fun isValid(value: String) = value.isEmpty() || value.isNotEmpty()
    }

  • @sterlingalbury9275
    @sterlingalbury9275 5 หลายเดือนก่อน

    Good video!

  • @aliaksandrbohush5257
    @aliaksandrbohush5257 5 หลายเดือนก่อน

    You are the King of Kotlin!

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Thank you, Aliaksandr! That's kind of you to say!

  • @Alchemist10241
    @Alchemist10241 5 หลายเดือนก่อน

    Your explanation was good but if you had given us more examples it would be awesome. but hey the tricks that you give us to ease our kotlin coding are unique and smart.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Hey, thanks for the feedback, Mike! 👍 I appreciate it!

  • @user-krnujdp
    @user-krnujdp 5 หลายเดือนก่อน

    Gold content.

  • @ArthurKhazbs
    @ArthurKhazbs 5 หลายเดือนก่อน

    Ha-ha, yes! I knew this video was going to have a "fun" twist!

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Haha, yeah! Sounds like you figured out where I was going with it! 🙂

  • @emmanuelmtali1594
    @emmanuelmtali1594 5 หลายเดือนก่อน

    Gold 🤑

  • @notramiras
    @notramiras 5 หลายเดือนก่อน

    Pokemon-card syndrome. When you can't infer the meaning of a term from the actual words, it's already a smell that you're in an overhead/buzzword-driven science culture. OOP is such a crippled world.

    • @typealias
      @typealias  5 หลายเดือนก่อน

      Yeah, it seems like many of the terms and phrases that stuck were rooted in a context that's been lost now. For example, I'm sure "inversion of control" made sense at the time, but once it became a more common practice, it's no longer an "inversion". It's sometimes also called the Hollywood Principle - "don't call us; we'll call you". I'm sure that idea works great if you can remember who is "us" and who is "you", but I never can 😁