It’s easy to understand pattern purpose. In our life we use adapters when connect from one plug type to socket with another type. When you use external libraries or connect your system to API, that could be created by other people especially if it was done many years ago, sometimes you need adapters. Let’s talk about this pattern on example.
Imagine, that you have IApi interface and NewApi Class that inherited this interface.
public interface IApi
{
public string GetJsonData();
}
public class NewApi : IApi
{
public string GetJsonData()
{
List<Doc> docs = new List<Doc>();
docs.Add(new Doc() { Num = "1", Content = "Doc numnber 1" });
docs.Add(new Doc() { Num = "2", Content = "Doc numnber 2" });
return JsonConvert.SerializeObject(docs, Formatting.Indented);
}
}
But, after several weeks or months your manager says that you should connect your system to some old API, that couldn’t return JSON. It can return XML only. But now you have your system and other system, that interact with each other. What can you do? Of course, you can create simple adapter. Let’s see.
We have OldApi class that doesn’t know about IApi interface and can’t return JSON.
public class OldApi
{
public string GetXmlData()
{
List<Doc> docs = new List<Doc>();
docs.Add(new Doc() { Num = "1", Content = "Doc numnber 1 (from Old system)" });
docs.Add(new Doc() { Num = "2", Content = "Doc numnber 2 (from old system)" });
var serializer = new XmlSerializer(typeof(List<Doc>));
StringBuilder sb = new StringBuilder();
using (var sw = new StringWriter(sb))
{
serializer.Serialize(sw, docs);
}
return sb.ToString() + System.Environment.NewLine;
}
}
We can create adapter class, that has constructor for OldAPI object. And new adapter class inherited IApi interface.
public class ApiAdapter : IApi
{
OldApi oldApi;
public ApiAdapter(OldApi oldApi)
{
this.oldApi = oldApi;
}
public string GetJsonData()
{
var serializer = new XmlSerializer(typeof(List<Doc>));
List<Doc> docList;
using (TextReader reader = new StringReader(oldApi.GetXmlData()))
{
docList = (List<Doc>)serializer.Deserialize(reader);
}
return JsonConvert.SerializeObject(docList, Formatting.Indented);
}
}
Let’s see client code. It’s very simple and easy to understand.
static void Main()
{
IApi newApi = new NewApi();
OldApi oldApi = new OldApi();
IApi apiAdapter = new ApiAdapter(oldApi);
Console.WriteLine(oldApi.GetXmlData()); //Only for demonstrating, we don't need to use this method
Console.WriteLine(newApi.GetJsonData());
Console.WriteLine(apiAdapter.GetJsonData());
}
And you can see results of our program:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfDoc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Doc>
<Num>1</Num>
<Content>Doc numnber 1 (from Old system)</Content>
</Doc>
<Doc>
<Num>2</Num>
<Content>Doc numnber 2 (from old system)</Content>
</Doc>
</ArrayOfDoc>
[
{
"Num": "1",
"Content": "Doc numnber 1"
},
{
"Num": "2",
"Content": "Doc numnber 2"
}
]
[
{
"Num": "1",
"Content": "Doc numnber 1 (from Old system)"
},
{
"Num": "2",
"Content": "Doc numnber 2 (from old system)"
}
]