Immutable Objects in Java

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 48

  • @fallenIights
    @fallenIights 4 года назад +3

    Watched all 30 of your FREE Java for Beginner videos. Great work, Marcus! 💯

    • @MarcusBiel
      @MarcusBiel  4 года назад +1

      Thanks. Currently I am working on a SaaS... One this is running, I want to do more videos. Did you also read the free book on my website? It has additional and updated info.

  • @otaldepeewee3596
    @otaldepeewee3596 Год назад

    I have two challenges in life, becoming fluent in English and learning Java. I currently do this at the same time.

  • @sermojohn
    @sermojohn 8 лет назад +3

    Thanks, great video! My question is: After you have created a lot of immutable instances of the same bank account, how do you know which is the actual balance, taking into account concurrency issues?

    • @MarcusBiel
      @MarcusBiel  8 лет назад

      Thanks for asking this again. For some weird reasons I hadn't see the earlier comment from Ioannis. I will answer right now!

    • @MarcusBiel
      @MarcusBiel  8 лет назад +2

      Well, in my course I have not talked about concurrency, which is why I haven't talked about concurrency related issues in this video either. Anyway - very good question. Everyone just states "Immutables can be shared freely without any synchronization" - but what this means is "without any lock or synchronizhed keyword" - from a business point of view, you still have to organize (synchronize) the immutable objects, that is correct. For this, you could use the Compare and Swap (CAS) algorithm. That can be quite easily done using an AtomicRefererence - docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicReference.html. So the AtomicReference instance acts as a "pointer" to the actual balance. Hope this helps!

    • @anoohemandas135
      @anoohemandas135 5 лет назад

      Marcus please include working Immutable bank examples at the end of each topic so that learning by example is easier

  • @mojtabavahdati7648
    @mojtabavahdati7648 3 года назад

    Thanks, this is really helpful Mr.Biel.
    Does the word "stable" mean "resistant against changing" and thus "resistant against getting into a wrong state" in your video?

  • @AS-lu7wl
    @AS-lu7wl 2 года назад

    HI Marcus, I finished these videos- What do I do next?

  • @dilneicunha
    @dilneicunha 8 лет назад

    Hi Marcus, great class! I always use immutable objects especially in my DTO's, like writing my views with DTO's, as I have no set methods the actual data value that enters the inputs is the real value that comes in the controllers. This seems perfect because we prevent the data being manipulated.

    • @MarcusBiel
      @MarcusBiel  8 лет назад +1

      Great that you liked it. As a quick recommendation, avoid the use of DTOs - they are just a hack, a helper mechanism, not very domain driven.

    • @dilneicunha
      @dilneicunha 8 лет назад

      Marcus, why avoid DTO's? if they are a pattern, and using them I can have my Immutable objects, always used DTO's web projects generally action-based projects, I use them to make my views because I can have a real separation of responsibilities between the view / controller and the layer of business. Could you explain me better?

    • @MarcusBiel
      @MarcusBiel  8 лет назад +2

      Well there are a lot of patterns that today are seen as an anti-pattern, for example also the singleton. With DTO's the reason you use them is usually because your entities become to complex to be transferred to the UI. But why are they too complex in the first place? Focusing on designing SIMPLE classes! So in your case, you have complex classes and you map them to simple DTOs to improve performance. But now you need a mapping class, and the mapping logic, which again costs performance (and code). Keep things simple in the first place, and you won't need this additional code. My recommendation is you read about "Domain Driven Design" - it might be a bit tough to understand at first, but it is REALLY worth it! www.amazon.com/gp/product/0134434420/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0134434420&linkCode=as2&tag=marcusbiel-20&linkId=3eeca79bf882f7fc556ea9c1468a3c5f

    • @dilneicunha
      @dilneicunha 8 лет назад

      Thanks for the explanation, and the book link, I'll read, sure, I agree with everything you said, but in parts :)
      For action-based applications, I still like to use DTO's even sometimes become a little more complex the application, in my case I have complex screens, usually I have other patterns associated with software architecture, such as assemblers I use to convert DTO's entities.
      gist.github.com/dilnei/39077f6e133b412a72f7
      gist.github.com/dilnei/175c1c9ee4def6d0b454
      But it is difficult to expose an entire architecture here for you, and talk about the reasons that led me to use this model, but expose objects (entities) directly on the view does not seem good, although I use exactly this way when the application faces (JSF ), I do not see how a good software design, because I lose immutability, due to having set methods for each entity's declared in the controller view.

  • @MezPahlan
    @MezPahlan 8 лет назад +2

    Great explanation! Really enjoyed that. Is the craftsman drawing of you?

    • @MarcusBiel
      @MarcusBiel  8 лет назад

      +Mez Pahlan sure, that is me. Compare the face with my photo! ☺️

    • @vasilchervenkov4633
      @vasilchervenkov4633 6 лет назад +1

      @@MarcusBiel haha

  • @St.Pa-al
    @St.Pa-al 4 года назад +1

    Thanks Marcus.

  • @subramanianchenniappan4059
    @subramanianchenniappan4059 5 лет назад +1

    Thanks. I am a java developer

  • @mannysingh6618
    @mannysingh6618 6 лет назад +1

    Have to be careful with immutables as if not used correctly, memory can be affected. It really depends on the type of system that you're designing..

    • @MarcusBiel
      @MarcusBiel  6 лет назад

      Actually I explained this in detail in the video:
      ruclips.net/video/-Tydziij7s4/видео.htmlm53s
      and here:
      ruclips.net/video/-Tydziij7s4/видео.htmlm13s

    • @mannysingh6618
      @mannysingh6618 6 лет назад +1

      Marcus Biel
      No worries, not being critical, like your videos, love to Germany 👍

    • @MarcusBiel
      @MarcusBiel  6 лет назад

      All good. Just wanted to clarify that this is part of the video already :)

  • @kaus05
    @kaus05 3 года назад

    Why was the balance field made final

  • @ahmeturun3153
    @ahmeturun3153 8 лет назад +1

    Nice explanation 👍 visuals are great

    • @MarcusBiel
      @MarcusBiel  8 лет назад

      Thanks. Great that you like it :)

    • @anoohemandas135
      @anoohemandas135 5 лет назад

      If you could post working examples of Immutable it would be great to understand.

  • @AlexHeavens
    @AlexHeavens 8 лет назад

    Thanks for the video :)
    This works great for simple classes, but do you have thoughts on how to construct an abstract class that is (if at least only by trust) immutable?
    One of the really big benefits of Java is it's extensibility, which a common abstract class aides. However, without trusting a factory passed to this common abstract class to always (scouts' honour) return a brand new object, I haven't thought how to achieve this yet...

    • @MarcusBiel
      @MarcusBiel  8 лет назад +1

      +Alexander Heavens 1. An abstract class is NEVER immutable. 2. Focus on designing simple classes. Not only for immutables, but generally. Complex is EVIL. We never want to have a complex class. 3. Inheritance is evil. This I haven't said in this video, but check out my video about inheritance. 4. Generally you can use any mutable class as IF it WAS immutable (just create a deep copy for every change).

  • @tech_excellence
    @tech_excellence 7 лет назад

    Hello Marcus, is this the same as The creational pattern --> immutable objects pattern ?

    • @MarcusBiel
      @MarcusBiel  7 лет назад +1

      +Efim Graur there is no creational pattern called immutable object. Compare:
      sourcemaking.com/design_patterns/creational_patterns
      You might also want to check out my latest video where I speak about immutable classes at TDC conference. Basically the same, just slightly updated and live spoken.

    • @tech_excellence
      @tech_excellence 7 лет назад

      Hello Marcus, thank you for your response, I asked this silly question because I read chapter 2 of
      www.amazon.com/OCP-Certified-Professional-Programmer-1Z0-809/dp/1119067901 and there it says that:
      ---> The next creational pattern we will discuss is the immutable objects pattern.
      Problem How do we create read-only objects that can be shared and used by multiple classes?
      Motivation Sometimes we want to create simple objects that can be shared across multiple classes, but for security reasons we don’t want their value to be modified. We could copy the object before sending it to another method ..............

    • @MarcusBiel
      @MarcusBiel  7 лет назад +1

      I am not sure if I really understand your question here. I assume you mean "How can we use immutable objects" concurrently? Well, immutable objects can be shared freely, without any kind of "technical synchronization" - but you still have to manage (synchronize) the global state between the different threads, yes. And yes, that's not easy, that's a rather advanced topic. In short, for optimal performance, you would use a lock-free algorithm in most cases. One such algorithm is the CAS - compare and swap algorithm. You have one object that holds the "global state" - and all other objects/thread will "optimistically" assume that no other thread has updated the state in between, and perform their own state change on the object. If however, they realize that the state was changed in between - they will rollback their change, retrieve the newest global state, and start over. This is a very rough quick explanation, but I hope this helps to understand the concept.
      This lockfree algorithm works perfect in low to medium contention - when there is low to medium "traffic" going on - for high contention, a locked algorithm where each thread has to wait for its turn (in a way like a singled threaded queue), is actually faster - compare that to a police man handling the traffic vs. traffic lights. The police man is best when there is low to medium traffic, the traffic light is best for high traffic, as its smart algorithms can handle the traffic much better - but might force cars to wait without any reason at nights. Anyway, back to Java - Java offers a number of "Atomic" classes to implement the CAS algorithm - check "AtomicInteger" for instance. Hope my eplanation helped more than it may have confused you:)

  • @harishchowdarysure8858
    @harishchowdarysure8858 7 лет назад

    I have one question we are using hp fortify tool it was showing cross browser scripting vat server side please find me one solution.

  • @tulsi6317
    @tulsi6317 8 лет назад

    I faced this question in interview panel.... Thanks

    • @MarcusBiel
      @MarcusBiel  8 лет назад

      Your welcome. Hope you were able to answer it BEFORE you watch this video also :)

    • @tulsi6317
      @tulsi6317 8 лет назад

      MrEternalFool in Ernst & Young Pvt. Ltd company

    • @tulsi6317
      @tulsi6317 8 лет назад

      Marcus Biel noo 😑😑

    • @MarcusBiel
      @MarcusBiel  8 лет назад

      Nooo what?

    • @tulsi6317
      @tulsi6317 8 лет назад

      That time, I am not aware of this question. I told we have to declare a class as final. 😃

  • @parmodprajapati
    @parmodprajapati 5 лет назад

    when we use immutable class with another example Please

  • @suar_pilla
    @suar_pilla 7 месяцев назад

    Gem!

  • @harishchowdarysure8858
    @harishchowdarysure8858 7 лет назад

    hi sir how r u?

  • @ThatThing1675
    @ThatThing1675 7 лет назад

    You speak very slowly. It drags the video out unnecessary and thus makes the video more boring and uninteresting. You should try increasing the tempo

    • @kiaramutschler9246
      @kiaramutschler9246 6 лет назад +4

      When learning something new I would say most people actually like it slow! If you don't you can always speed the video up. I watched it at 1.25 speed :)

    • @MarcusBiel
      @MarcusBiel  6 лет назад +3

      Exactly, thanks Kiara. Just speed up the video, and the problem should be solved for you.

    • @jvsnyc
      @jvsnyc 4 года назад

      @@MarcusBiel Agreed that it is asymmetrical. If one does a mellow, paced video, speeding it up by watching at 1.25, 1.5, 1.75 or even 2x works fine. If one is too frenetic and rushing, not only is it more stressful, and more likely you will make mistakes and need to edit, but it really does NOT work to watch at 0.5 or 0.25 speed for those who need it slower. So this is an example of something superficially looking symmetrical that is not. Slow and steady wins the race.