Decorator vs Adapter pattern
I wonder what the difference between the two is
Match interfaces of different classes
Decorator: Add responsibilities to objects dynamically
5 Replies
Decorator
Adapter
I personally use that logging adapter in cases where I have to test
_logger.LogInformation
, because you know LogInformation
is an extension method and it doesn't inherit from an interface, so you can't test it. The LoggingAdapter
makes it testable.
I kinda use the Decorator above to extend the capabilities of the ConnectionFactory
and in this case to add configuration from appsettings or Action<T>
Another use case of mine where my code depends on DynamoDbClientFactory.GetClientAsync
which is asynchronous and is coming from an external library, meaning I cannot really make it synchronous for the AddSingleton<T> ctor, so I needed a workaround and there it is
so I'm not sure how to distinguish between the two: Decorator and Adapter
In my opinion, the intent of Adapter is not to add feature (Decorator), but to convert old feature to a new interface
what do you think?Common theoretical examples of decorators are things like coffee or pizza. Pizza class, large pizza class, ham and pineapple pizza class, etc. instead of multiple hundreds of classes a few classes are used to decorate functionality.
Adapter is different, it is designed to facilitate communication between classes. You might have different versions of a database to interact with, different logging systems etc.
that's a bit confusing to me
https://ahmedabdelkarim.wordpress.com/2019/11/21/facade-vs-adapter-vs-decorator-design-pattern/
What part is confusing? Can you elaborate on where you are confused?
NB: a lot of design patterns have similar goals and attributes. I encourage people to learn about various design patterns; but there are very few pure situations that fit a problem to a pattern so don't force your problem into rigid pre-defined solutions; instead take the parts that matter for your problem and use them as you need.
thanks
that's what I did in the examples above