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.
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?
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!
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?
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.
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?
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
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.
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...
+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).
+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.
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 ..............
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:)
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 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.
Watched all 30 of your FREE Java for Beginner videos. Great work, Marcus! 💯
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.
I have two challenges in life, becoming fluent in English and learning Java. I currently do this at the same time.
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?
Thanks for asking this again. For some weird reasons I hadn't see the earlier comment from Ioannis. I will answer right now!
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!
Marcus please include working Immutable bank examples at the end of each topic so that learning by example is easier
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?
HI Marcus, I finished these videos- What do I do next?
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.
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.
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?
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
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.
Great explanation! Really enjoyed that. Is the craftsman drawing of you?
+Mez Pahlan sure, that is me. Compare the face with my photo! ☺️
@@MarcusBiel haha
Thanks Marcus.
Thanks. I am a java developer
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..
Actually I explained this in detail in the video:
ruclips.net/video/-Tydziij7s4/видео.htmlm53s
and here:
ruclips.net/video/-Tydziij7s4/видео.htmlm13s
Marcus Biel
No worries, not being critical, like your videos, love to Germany 👍
All good. Just wanted to clarify that this is part of the video already :)
Why was the balance field made final
Nice explanation 👍 visuals are great
Thanks. Great that you like it :)
If you could post working examples of Immutable it would be great to understand.
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...
+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).
Hello Marcus, is this the same as The creational pattern --> immutable objects pattern ?
+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.
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 ..............
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:)
I have one question we are using hp fortify tool it was showing cross browser scripting vat server side please find me one solution.
I faced this question in interview panel.... Thanks
Your welcome. Hope you were able to answer it BEFORE you watch this video also :)
MrEternalFool in Ernst & Young Pvt. Ltd company
Marcus Biel noo 😑😑
Nooo what?
That time, I am not aware of this question. I told we have to declare a class as final. 😃
when we use immutable class with another example Please
Gem!
hi sir how r u?
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
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 :)
Exactly, thanks Kiara. Just speed up the video, and the problem should be solved for you.
@@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.