Skip to content
Carmel Eve By Carmel Eve Software Engineer I
Design patterns in C# - The Adapter Pattern

Hello again, in this post I'm continuing my series on design patterns in C#, this time focusing on the adaptor pattern!

The adapter pattern is useful when you want to use a class that does not fit the design of your existing solution. This is often the case when using legacy or external code. The adapter pattern allows you to define a wrapper which executes the desired behaviour, but exposes it through a method which your solution expects.

As always, I think this will become clearer when we delve into our (dinosaur-based) example.

Adapter Pattern Example

So, today we've been transported to a post-meteor world...

And in this world, in the age of mammals, we have a ChildCreator that expects things to work in a certain way:

Where the IMammal interface looks like this:

The GiveBirth method produces an IChild, which has a single method, Cry (which I'm pretty sure is the main functionality of a new born baby, right?).

However, though the world has moved on, we have a few legacy Triceratops hanging around, that are just trying to get along in an unfamiliar place... Unfortunately a Triceratops works somewhat differently to the majority of mammals (being a reptile and all), possessing a single LayEgg method:

The TriceratopsEgg produced by this method then has the ability to hatch into a TriceratopsChild:

And here is where our adapter comes in. We need a way that our ChildCreator can work with the Triceratops to produce a child.

If we define a TriceratopsToMammalAdapter, which implements the IMammal class, and wraps an internal Triceratops:

Then the GiveBirth method can call the LayEgg method of the internal Triceratops, and then Hatch that egg to produce an IChild. We then return the child, and the Triceratops is able to continue to function in the new mammalian landscape.

If we now use our TriceratopsToMammalAdapter and ChildCreator:

We can produce a healthy, crying, TriceratopsChild, with the output: TRICERATOPS IS CRYING! (Granted, as the lack of modern triceratops indicates, this probably isn't a long term solution and eventually that legacy code will likely still need to be replaced...)

Thanks for reading! Here's a link to the GitHub repository which contains all the code for this blog series. And watch of for my next blog on design patterns in C#!

Carmel Eve

Software Engineer I

Carmel Eve

Carmel has recently graduated from our apprenticeship scheme.

Over the past four years she has been focused on delivering cloud-first solutions to a variety of problems. These have ranged from highly-performant serverless architectures, to web applications, to reporting and insight pipelines and data analytics engines. She has been involved in every aspect of the solutions built, from deployment, to data structures, to analysis, querying and UI, as well as non-functional concerns such as security and performance.

Throughout her apprenticeship, she has written many blogs, covering a huge range of topics. She has also given multiple talks focused on serverless architectures. The talks highlighted the benefits of a serverless approach, and delved into how to optimise the solutions in terms of performance and cost.

She is also passionate about diversity and inclusivity in tech. Last year, she became a STEM ambassador in her local community and is taking part in a local mentorship scheme. Through this work she hopes to be a part of positive change in the industry.

Carmel won "Apprentice Engineer of the Year" at the Computing Rising Star Awards 2019.