5 Ways to Implement Adapter Design Pattern in C# 10

With real-world examples.

Sasha Marfut
CodeX

--

Photo by ConvertKit on Unsplash

Probably none of the existing design patterns has a single and strict implementation that a developer must follow. Typically, design patterns just describe a general idea that can be implemented in different ways depending on each specific case, requirements, technical constraints, etc.

The adapter design pattern is no exception as it has at least 5 different implementations that we are going to look at.

Example Definition

Imagine that we are developing an online store that has some discount system for customers. Here’s what the code responsible for calculating discounts might look like:

Key points:

  • Money value object is defined as a custom data type in order to avoid a primitive obsession problem.
  • IDiscountCalculator takes a product price as input and returns the new price after the discount factor has been applied.
  • DiscountCalculatorFactory class defines a…

--

--

Sasha Marfut
CodeX
Writer for

1 million+ views on Medium | .NET, Design Patterns, Architecture, API