😲😲 My new course has launched! You can check out Refactoring for C# Devs on Dometrain: dometrain.com/course/from-zero-to-hero-refactoring-for-csharp-developers 📌 Remember to subscribe to my free weekly software engineering newsletter: subscribe.devleader.ca
This is a great question. In fact, it absolutely could be static. However - You'll find in nearly all of the content I produce that I don't show usage of static classes. Some exceptions are utility or extension methods, or in mostly edge cases where composition doesn't work. The reason I avoid them, in general, is to help support testability. In this simple example we could certainly get away with it. I don't have a strong argument here. However, if our classes were slightly more complex and you wanted to start writing tests on them, static classes make this a total pain in the butt. They're shared across the entire runtime of the app/tests, and if they have state that can be mutated it's game over. If they interact with external systems, you have no choice but to interact with those systems (no mocking or otherwise intercepting in tests). I hope that helps explain why I generally won't do that (but you're certainly correct that it would be functional).
@@VishnuPriya-ng6wk when they are normal classes with interfaces we can use mocking frameworks. The mock only needs to meet the interface and not the concrete class, and since static classes cannot have interfaces you don't get this luxury. Static classes make things more challenging to test based on how complex they are. If you have simple methods, truly it's not very difficult. But if you are executing a lot of code in a static method, consider that anyone trying to write a unit test will not have *any* control over that static method. They need to have everything set up properly for the static code to execute properly It can get so complex that it makes writing unit tests difficult, or brittle/flakey when running in parallel. It's not to say they cannot be used, but I've seen too many instances where they cause a ton of headaches. I design to not use them, and when I do, they're very lightweight 🙂
Thanks so much! And you're right - the user name thing was a bit of a gotcha moment when I was recording this thinking I might not have planned to cover that well enough. Poor planning on my part 😕
Let me fix that for you: "Thanks for putting together a free resource on this. It seems like there are two really common examples that everyone seems to show though: chat room and air traffic controller. Do you think you could put together a helpful tutorial with scenario X?" (Or simply ask about another). Hope that helps for next time. The amount of effort I want to put into a mediator tutorial now is approximately zero.
😲😲 My new course has launched! You can check out Refactoring for C# Devs on Dometrain:
dometrain.com/course/from-zero-to-hero-refactoring-for-csharp-developers
📌 Remember to subscribe to my free weekly software engineering newsletter:
subscribe.devleader.ca
It would be perfect if you had tutorials regarding all design patterns,really look forward to watching all the high quality educational videos!
@@liangbinyu7620 I started off in that direction 😅 it's even on my whiteboard to get through more of them.
Thanks for the feedback!
Why should ChatUser have an instance of mediator? why cant it be a static class?
This is a great question. In fact, it absolutely could be static.
However - You'll find in nearly all of the content I produce that I don't show usage of static classes. Some exceptions are utility or extension methods, or in mostly edge cases where composition doesn't work.
The reason I avoid them, in general, is to help support testability. In this simple example we could certainly get away with it. I don't have a strong argument here. However, if our classes were slightly more complex and you wanted to start writing tests on them, static classes make this a total pain in the butt. They're shared across the entire runtime of the app/tests, and if they have state that can be mutated it's game over. If they interact with external systems, you have no choice but to interact with those systems (no mocking or otherwise intercepting in tests).
I hope that helps explain why I generally won't do that (but you're certainly correct that it would be functional).
@@DevLeader , thanks for the reply. Why are static classes hard to test? In other words, how making them non static helps with mocking?
@@VishnuPriya-ng6wk when they are normal classes with interfaces we can use mocking frameworks. The mock only needs to meet the interface and not the concrete class, and since static classes cannot have interfaces you don't get this luxury.
Static classes make things more challenging to test based on how complex they are. If you have simple methods, truly it's not very difficult. But if you are executing a lot of code in a static method, consider that anyone trying to write a unit test will not have *any* control over that static method. They need to have everything set up properly for the static code to execute properly
It can get so complex that it makes writing unit tests difficult, or brittle/flakey when running in parallel.
It's not to say they cannot be used, but I've seen too many instances where they cause a ton of headaches. I design to not use them, and when I do, they're very lightweight 🙂
Great content, Nick. I would first normalize the user Name property at 9:31
Thanks so much!
And you're right - the user name thing was a bit of a gotcha moment when I was recording this thinking I might not have planned to cover that well enough. Poor planning on my part 😕
Thanks. That was interesting.
Glad that you enjoyed it!
Is a chatroom and an air traffic controller the only use for this damn pattern? Can any developer out there think of a different use. holy smokes
Let me fix that for you:
"Thanks for putting together a free resource on this. It seems like there are two really common examples that everyone seems to show though: chat room and air traffic controller. Do you think you could put together a helpful tutorial with scenario X?" (Or simply ask about another).
Hope that helps for next time. The amount of effort I want to put into a mediator tutorial now is approximately zero.