No video

Use the Decorator Pattern To Reduce Code Duplication in Complex Models

  Рет қаралды 22,661

Zoran Horvat

Zoran Horvat

Күн бұрын

Download the source code: / source-code-for-86997277
There is an ever-lasting issue of extending the behavior of classes while not having to change their implementation. In this video, we delve into one of the solutions - the Decorator design pattern. A cornerstone of object-oriented design principles such as the Single Responsibility Principle, the Open Closed Principle, and the Liskov Substitution Principle, the Decorator pattern offers a powerful approach to modifying an object's behavior without changing its class.
The Decorator steps in to add or enhance the functionalities of an object around the execution of a specific method. That allows the object to continue performing its core role while the Decorator adds to or alters its behavior.
A significant advantage of the Decorator pattern is its ability to combat logic duplication. Through a meticulous application of the decorator in the demo code, we will remove duplicated clauses from all subject classes. The decorator will effectively add the shared responsibility uniformly across all the relevant implementations of an interface.
In essence, the Decorator stands as an intermediary, letting the original classes operate as intended while ensuring that shared logic (such as excluding certain conditions) is managed consistently across all instances.
By improving modularity and reducing duplication, this tool can significantly enhance the quality of your code. We encourage you to dive into this pattern and leverage it in your work.
Thank you so much for watching! Please like, comment & share this video as it helps me a ton!! Don't forget to subscribe to my channel for more amazing videos and make sure to hit the bell icon to never miss any updates.🔥❤️
✅🔔 Become a patron ► / zoranhorvat
✅🔔 Subscribe ► / @zoran-horvat
⭐ Learn more from video courses:
Beginning Object-oriented Programming with C# ► codinghelmet.c...
⭐ Collections and Generics in C# ► codinghelmet.c...
⭐ Making Your C# Code More Object-oriented ► codinghelmet.c...
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️ Have a look at our other Videos :
👉 Using GitHub Copilot to Write Complex Code | Step-by-step Tutorial ► • Using GitHub Copilot t...
👉 Coding with GitHub Copilot - Beginner to Master | VS Code Demo ► • A Comprehensive Guide ...
👉 What is Covariance and Contravariance in C# ► • What is Covariance and...
How to Initialize a Clean ASP.NET Core Project with Entity Framework Core and Identity ► • How to Initialize a Cl...
👉 The Null Conundrum: A Guide to Optional Objects in C# ► • How to Avoid Null Refe...
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⭐ CONNECT WITH ME 📱👨
🌐Become a patron ► / zoranhorvat
🌐Buy me a Coffee ► ko-fi.com/zora...
🗳 Pluralsight Courses ► codinghelmet.c...
📸 Udemy Courses ► codinghelmet.c...
📸 Join me on Twitter ► / zoranh75
🌐 Read my Articles ► codinghelmet.c...
📸 Join me on LinkedIn ► / zoran-horvat
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
👨 About Me 👨
Hi, I’m Zoran, I have more than 20 years of experience as a software developer, architect, team lead, and more. I have been programming in C# since its inception in the early 2000s. Since 2017 I have started publishing professional video courses at Pluralsight and Udemy and by this point, there are over 100 hours of the highest-quality videos you can watch on those platforms. On my KZfaq channel, you can find shorter video forms focused on clarifying practical issues in coding, design, and architecture of .NET applications.❤️
▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬
⚡️RIGHT NOTICE:
The Copyright Laws of the United States recognize a “fair use” of copyrighted content. Section 107 of the U.S. Copyright Act states: “Notwithstanding the provisions of sections 106 and 106A, the fair use of a copyrighted work, including such use by reproduction in copies or phono records or by any other means specified by that section, for purposes such as criticism, comment, news reporting, teaching (including multiple copies for classroom use), scholarship, or research, is not an infringement of copyright." This video and our youtube channel, in general, may contain certain copyrighted works that were not specifically authorized to be used by the copyright holder(s), but which we believe in good faith are protected by federal law and the Fair use doctrine for one or more of the reasons noted above.
⭐For copyright or any inquiries, please contact us at zh@codinghelmet.com
#csharp #dotnet #designpatterns

Пікірлер: 15
@VoldHooker
@VoldHooker Жыл бұрын
I appreciate the way you present these patterns (and topics in general). Another well structured video, thank you.
@faisalst32
@faisalst32 Жыл бұрын
We need you here in the wolrd of TypeScript
@koderkev42
@koderkev42 Жыл бұрын
Hidden complexity is often in the root-cause tree of bugs. The pattern definitely has use-cases but trying to read code with in a browser or basic text editor becomes more challenging. In an IDE, it's simple enough to navigate through member/object definitions but not so for non-IDE cases, such as Azure DevOps Repos (although there is some support for code searching on enabled branches). Thanks for sharing!
@numannebuni
@numannebuni Жыл бұрын
This is quite interesting and took me by surprise, tbh. Very interesting. Thanks!
@fadge316
@fadge316 Жыл бұрын
Just became a sponsor ✌️
@zoran-horvat
@zoran-horvat Жыл бұрын
Thanks!
@fadge316
@fadge316 Жыл бұрын
@@zoran-horvat thank you 🙂 I love your channel, even though watching you code makes me feel like I know nothing about C# 😂
@vyslr
@vyslr Жыл бұрын
Thanks! I have one question tho. What if we needed to implement more than one logic just like what you did with removing zero discounts?
@zoran-horvat
@zoran-horvat Жыл бұрын
Implement it in another decorator and put it around this one!
@protox4
@protox4 Жыл бұрын
You actually just changed the behavior. The `yield break` inside the loop exited both loops, while your new method continues to get more items.
@zoran-horvat
@zoran-horvat Жыл бұрын
Yes, but the resulting sequence is the same, isn't it?
@zoran-horvat
@zoran-horvat Жыл бұрын
BTW, what do you mean by "new method continues to get more items". The producing method is returning a lazy-evaluated sequence. The calling method doesn't "get" the items, but instead it pulls them. Therefore, a break in the calling method would terminate the producing loops - both nested loops.
@protox4
@protox4 Жыл бұрын
@@zoran-horvat Not at all. You could've used `TakeWhile` instead of `Where`, but even then it's not comparing the same thing. The loop is comparing a local variable that's being decremented, while your linq method is comparing the yielded items.
@zoran-horvat
@zoran-horvat Жыл бұрын
Alright, I had to read the code after all - forgive me, I was recording this video some time ago and couldn't recall all the details without watching the critical part. You are right that the decorator will pull all the items in the first sequence, while the initial solution would stop at the first zero. However, the resulting sequence will be equal. The producer will start producing zeros at some point and the decorator will just trim them. The TakeWhile is not appropriate here because we don't know for sure that all decorated objects will produce zeros at the end of the sequence. Even though it could be more efficient, such a decorator would only be applicable to implementations with a stronger contract. With only the IDiscount as the contract, the decorator should not stop at the first zero because non-zero values might appear later. The proposition here is not to preserve the exact behavior (this is not a refactoring), but only to preserve the externally observable behavior. Therefore, the solution from the end of the video should be correct for all implementations of the IDiscount interface.
Cute kitty gadgets 💛
00:24
TheSoul Music Family
Рет қаралды 17 МЛН
🩷🩵VS👿
00:38
ISSEI / いっせい
Рет қаралды 27 МЛН
English or Spanish 🤣
00:16
GL Show
Рет қаралды 8 МЛН
这三姐弟太会藏了!#小丑#天使#路飞#家庭#搞笑
00:24
家庭搞笑日记
Рет қаралды 94 МЛН
How to use the Decorator Pattern (Card Game Example)
14:55
git-amend
Рет қаралды 7 М.
Do You Really Need a Data Transfer Object?
9:57
SingletonSean
Рет қаралды 6 М.
Decorator Pattern in Swift
5:53
AF Swift Tutorials
Рет қаралды 1,9 М.
Favor Method Chaining Over Nested Calls | Clean Code
10:23
Zoran Horvat
Рет қаралды 25 М.
Let C# Tuples Become Your Best Friends
11:51
Zoran Horvat
Рет қаралды 10 М.
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,2 МЛН
Why is C# Evolving This Way?
15:02
Zoran Horvat
Рет қаралды 21 М.
Wish To Remove Primitive Obsession? Try This Effective Technique!
15:28
Decorator Pattern - Design Patterns (ep 3)
54:35
Christopher Okhravi
Рет қаралды 481 М.
17 Pieces of C# Syntax That Make Your Code Short
12:41
Zoran Horvat
Рет қаралды 22 М.
Cute kitty gadgets 💛
00:24
TheSoul Music Family
Рет қаралды 17 МЛН