In this article, as is clear from the title today is about the Adapter design pattern, the name of which can be guessed enables the cooperation of objects that do not match each other 🙂

Description and implementation method

More specifically, in the Adapter pattern, the idea is to make it possible for objects with incompatible interfaces to work together. It is useful, for example, in the use of newer versions of some libraries, eg when we used an old library, but a new library came out and we want to adapt this old one to this new one. Another task of the Adapter is also to pack the old interface into a new one.

In the real world, the adapter is like an adapter to the monitor plug, for example, I recently had to buy a new monitor and the monitor plug did not fit into the computer, so I had to go to the store for an adapter. The adapter has a very similar function in the programming world.

Structure

Let’s look at the UML diagram of the adapter pattern

It shows that the Rectangle class uses the Shape interface and the LegacyRectangle class uses the Rectangle class.

Example

The scheme of operation

Let’s take an example to show what the adapter is about:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

classAdapter:IClientInterface

{

publicvoidSomeMethod1()

{

varoldclass=newNewClass();

oldclass.SomeMethod2();//Calling the SomeMethod2 method

}

}

publicinterfaceIClientInterface

{

voidSomeMethod1();

}

classNewClass

{

publicvoidSomeMethod2()

{

Console.WriteLine("Calling the SomeMethod2 method");

}

}

staticvoidMain(string[]args)

{

Adapter adapter=newAdapter();

adapter.SomeMethod1();//Calling the SomeMethod1 method

Console.ReadKey();

}

I did it on a very simple example, there is a new class and we want it to be compatible with the old class interface without changing the method names, I created for this purpose an adapter class that implements the IClientInterface interface, i.e. we have to create old methods in this class and so we do that and inside SomeMethod1() method create an instance of a new class (let’s not focus on the rules of pure code, it’s important to convey the idea of the adapter) and calls its method.

Notice what happened, we used the new class method, but in the Adapter class, which uses the names of the old class methods, this is more or less what the adapter does.

Result:

Phone models

Let’s do an example now that better applies to everyday life, let’s do an adapter for the new version of iphone from 6 to 7:

The iphone6 class looks like this:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

interfaceIiphone6

{

voidProtectedFromWater(boolifprotected);

voidSpeedWorking(stringspeed);

voidSizeOfScreen(intinch);

}

classIphone6:Iiphone6

{

publicvoidSizeOfScreen(intinch)

{

Console.WriteLine("Screen size in inches: "+inch);

}

publicvoidSpeedWorking(stringspeed)

{

Console.WriteLine("IPhone speed: "+speed);

}

publicvoidProtectedFromWater(boolifprotected)

{

Console.WriteLine("Is it protected from water? "+ifprotected);

}

}

And the iphone7 class like this:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

interfaceIiphone7

{

voidPhoneIsProtectedFromWater(boolifprotected);

voidSpeedPhoneWorking(stringspeed);

voidPhoneSizeScreen(intinch);

}

classIphone7:Iiphone7

{

publicvoidPhoneSizeScreen(intinch)

{

Console.WriteLine("Screen size in inches: "+inch);

}

publicvoidSpeedPhoneWorking(stringspeed)

{

Console.WriteLine("IPhone speed: "+speed);

}

publicvoidPhoneIsProtectedFromWater(boolifprotected)

{

Console.WriteLine("Is it protected from water? "+ifprotected);

}

}

We can see that the interfaces of these classes do not match, have different method names, method names in the iphone6 and iphone7 classes, also changed, and we want to use old method names, in which case we need to create an adapter that will be implementing methods with old names and in these methods he will call methods from the new class iphone7, it looks like this:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

classIphoneAdapter:Iiphone6

{

Iiphone7 iphone7=newIphone7();

publicvoidProtectedFromWater(boolifprotected)

{

iphone7.PhoneIsProtectedFromWater(ifprotected);

}

publicvoidSizeOfScreen(intinch)

{

iphone7.PhoneSizeScreen(inch);

}

publicvoidSpeedWorking(stringspeed)

{

iphone7.SpeedPhoneWorking(speed);

}

}

First, we create an instance of the iphone7 class with an abstract type, then we implement the methods from the iphone6 interface and in them we call methods from the new class.

In the client, in the “Main” method, we call it like this:

C#

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

staticvoidMain(string[]args)

{

Console.WriteLine("iphone6 parameters");

Iphone6 iphone6=newIphone6();

iphone6.ProtectedFromWater(true);//Is it protected from water? true

iphone6.SizeOfScreen(3);//Screen size in inches: 3

iphone6.SpeedWorking("Slow");//IPhone speed: Slow

Console.WriteLine("\niphone7 parameters");

IphoneAdapter iphone6Adapter=newIphoneAdapter();

iphone6Adapter.ProtectedFromWater(true);//Is it protected from water? true

iphone6Adapter.SizeOfScreen(4);//Screen size in inches: 4

iphone6Adapter.SpeedWorking("Fast");//IPhone speed: Fast

Console.ReadKey();

}

First, we call the old version of iphone6, then the Adapter class, which has old methods of the iphone6 class, in both cases works the same.

Result:

Change of the electricity system

This example had to be found, in this example we will switch from the Polish electricity system to the American one, this is well illustrated by the picture below.

Let’s move to the code, this example will look very much like the previous one with phones. Let’s start with the PolandElectricalSocket class, ie from the Polish electricity system.

C#

1

2

3

4

5

6

7

8

9

10

11

12

publicinterfacePolandPlugConnector

{

voidgiveElectricity();

}

publicclassPolandElectricalSocket:PolandPlugConnector

{

publicvoidgiveElectricity()

{

Console.WriteLine("Use electricity systems from Poland");

}

}

And let’s also see what the class from the American electricity system looks like.

C#

1

2

3

4

5

6

7

8

9

10

11

12

publicinterfaceUKPlugConnector

{

voidprovideElectricity();

}

publicclassUKElectricalSocket:UKPlugConnector

{

publicvoidprovideElectricity()

{

Console.WriteLine("Use electricity systems from USA");

}

}

And let’s see the class of the adapter switching the Polish system to the American system.