...

/

Adapter: Implementation and Example

Adapter: Implementation and Example

Learn the Adapter design pattern by implementing an example program.

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:

Press + to interact
namespace Adapter_Demo;
internal interface IElectricSocket
{
}

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:

Press + to interact
namespace Adapter_Demo;
internal interface ISocketPlug
{
void SelectSocket(IElectricSocket socket);
void ConnectToSocket();
}

This interface contains two methods given on lines 5–6, ...