Adapter: Implementation and Example
Explore how to implement the Adapter design pattern in C#. Understand how to create interfaces for European and British electric sockets and develop an adapter that allows these incompatible types to work together. This lesson demonstrates practical usage of the Adapter pattern to solve real-world compatibility challenges in software design.
Implementing the Adapter pattern
In this example, we’ll use the concept of electric sockets. We’ll mimic the use of a socket adapter in the code.
Creating a standard application
We‘ll create a standard .NET console application. Then, we’ll add the IElectricSocket.cs file to it. The file will contain the following empty interface definition:
Implementing a socket plug interface
Then, we’ll create an interface representing a socket plug. For this, we’ll add the ISocketPlug.cs file with the following content:
This interface contains two methods given on lines 5–6, in the code above:
- The
SelectSocket()method, which