This newsletter is brought to you by:
Pragmatic Clean Architecture V2 , This course will teach you everything you need to build production-ready applications with Clean Architecture.
Here's what's new:
More than 3.5h+ of new content
Brand new Authorization section
Revamped Advanced Topics section
New and improved Testing section
Shiny new bonus content section
All source code updated to .NET 8
HURRY UP, Current Price is $150, it will increase to $247 in two days
Adapter Design Pattern
Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
Analogy
Imagine you have different electronic devices with various power plugs, and you want to connect them all to a single power outlet.
Instead of modifying the devices, you use plug adapters. These adapters act as intermediaries, allowing each device to connect to the outlet seamlessly.
Similarly, the Adapter Design Pattern lets incompatible classes work together by providing a wrapper (adapter) that bridges the gap between their interfaces.
When should we use it ?
Utilize the Adapter class when you need to incorporate an existing class into your code, but its interface doesn't align with the rest of your system.
Advantages and Disadvantages
By using adapter design pattern we fulfill S and O from SOLID Principles , although in some situations it can increase the complexity of code.
Enough talk, let’s see code in action.
How to implement Adapter in C#
Suppose we have some old code with in compatible interfaces.
And now we have a new interface expected by the rest of the code
Let’s make an adapter class that makes OldSystem compatible with INewSystem
Let’s see now how can we utilise it.
If you wanna learn more about design patterns here are three awesome resources
P.S: These are not promotional links at all, I personally found them helpful
Whenever you’re ready, there are 2 ways I can help you:
Promote yourself to 9300+ subscribers by Sponsoring my Newsletter
Previous sponsors : Jetbrains, Workflow Engine
Become a Patron and get access to 200+ .NET Questions and Answers