Adapter Design Pattern Real-time Example

Adapter Design Pattern Real-time Example in C#

In this article, I am going to discuss the Adapter Design Pattern Real-time Example in C#. Please read our previous article where we discussed the basics of the Adapter Design Pattern. Here, in this article, we will implement Language Translator Example using Adapter Design Pattern which is one of the best Real-time examples of the Adapter Design Pattern.

Adapter Design Pattern Real-time Example- Language Translator

Please have a look at the following diagram. On the left-hand side, you can see a person called John and on the right-hand side, you can see a person called David.

Adapter Design Pattern Real-time Example in C#

The person John is from the USA and he can only speak and understand only English. On the other hand, the person David is from France and he can only speak and understand French. Now, John wants to speak something to David and David wants to speak something to John. But, currently, it is not possible because John only knows English and he can’t understand French. Like that David knows only French and he can’t understand English.

So the above two interfaces are incompatible as they can’t communicate.

How to make them compatible?

In order to make them compatible with each other, what we need to do is, we need to introduce a translator between John and David which is shown in the below image. Here, Pam acts as a Translator. Pam knows both English and French. She can speak and understand English, as well as translating English to French. She also speaks and understands French, as well as translate French to English. So, here, Pam is nothing but an Adapter.

Adapter Design Pattern Real-time Example- Language Translator

Suppose John wants to ask how are you to David. Then what John will do is, he will ask how are you to Pam and Pam will convert how are you to French and ask David. David will reply in French to Pam and Pam again converts that French to English and return to John. So, in this case, both John and David will communicate with each other. Here, the important thing is John and John is acting as an Adapter.

Implementation of Adapter Design Pattern Real-time Example:

Let us implement the above-discussed Language Translator Example step by step in C# using Adapter Design Pattern.

As we already discussed in our previous article, that the Adapter Design Pattern involves four components (Target, Client, Adaptee, and Adapter). Let us create these components by comparing them with our example.

Step1: Creating EnglishSpeaker Interface (Adaptee)

Create an interface with the name IEnglishSpeaker and then copy and paste the following code in it. This interface is having two methods i.e. AskQuestion and AnswerFortheQuestion and both these methods expect parameter words.

namespace AdapterDesignPatternRealTimeExample
{
    public interface IEnglishSpeaker
    {
        string AskQuestion(string Words);
        string AnswerFortheQuestion(string Words);
    }
}

In our example, John is the person who can speak and understand only English. So create a class file with the name John.cs and then copy and paste the following code in it. This John class implements the IEnglishSpeaker interface and provides implementation for the two abstract methods i.e. AskQuestion and AnswerFortheQuestion.

using System;
namespace AdapterDesignPatternRealTimeExample
{
    // John is from USA, So he can speak and understand only English
    public class John : IEnglishSpeaker
    {
        public string AskQuestion(string Words)
        {
            Console.WriteLine("Question Asked by John [English Speaker and Can understand only English] : " + Words);
            ITarget target = new Pam();
            string replyFromDavid = target.TranslateAndTellToOtherPerson(Words, "French");
            return replyFromDavid;
        }
        
        public string AnswerFortheQuestion(string Words)
        {
            string reply = null;
            if (Words.Equals("where are you?", StringComparison.InvariantCultureIgnoreCase))
            {
                reply = "I am in USA";
            }
            return reply;
        }
    }
}
Step2: Creating FrenchSpeaker Interface (Adaptee)

Create an interface with the name IFrenchSpeaker and then copy and paste the following code in it. This interface is also having two methods (AskQuestion and AnswerFortheQuestion) and again these two methods expect parameter words.

namespace AdapterDesignPatternRealTimeExample
{
    public interface IFrenchSpeaker
    {
        string AskQuestion(string Words);
        string AnswerFortheQuestion(string Words);
    }
}

In our example, David is the person who can only speak and understand only French. So, create a class with the name David and then copy and paste the following code in it. This class is going to implement the IFrenchSpeaker interface and provides implementations for the AskQuestion and AnswerFortheQuestion abstract methods.

using System;
namespace AdapterDesignPatternRealTimeExample
{
    // David is from France and can speak and understand only French
    public class David : IFrenchSpeaker
    {
        public string AskQuestion(string Words)
        {
            Console.WriteLine("Question Asked by David [French Speaker and Can understand only French] : " + Words);
            ITarget target = new Pam();
            string replyFromJohn = target.TranslateAndTellToOtherPerson(Words, "English");
            return replyFromJohn;
        }

        public string AnswerFortheQuestion(string Words)
        {
            string reply = null;
            if (Words.Equals("comment allez-vous?", StringComparison.InvariantCultureIgnoreCase))
            {
                reply = "Je suis très bien";
            }
            return reply;
        }
    }
}
Step3: Creating Target interface

Create an interface with the name ITarget and then copy and paste the following code in it. The ITarget interface defines the abstract TranslateAndTellToOtherPerson method which is going to be implemented by the Adapter.

namespace AdapterDesignPatternRealTimeExample
{
    public interface ITarget
    {
        string TranslateAndTellToOtherPerson(string words, string convertToWhichLanguage);
    }
}
Step4: Creating Pam (Translator or Adapter)

Create a class with the name Pam and then copy and paste the following code in it. The following Pam class implements the ITarget interface and provides implementations for the TranslateAndTellToOtherPerson method. What this TranslateAndTellToOtherPerson method does is, it will translate the word to English and talk to John and again it also translates the word to French and talk to David. This method acts as a two-way communicator (it can communicate with both persons).

This class is also having two methods. One method is used to convert English to French and the other method is used to convert French to English.

Note: This class act as an Adapter as it is adapting something before communicating.

Step5: Client

Our main method is going to be the client for our application. So, please modify the Main method as shown below.

Output:

Implementation of Adapter Design Pattern Real-time Example

In the next article, I am going to discuss the Facade Design Pattern in C# with real-time examples. Here, in this article, I try to explain one of the best Adapter Design Pattern Real-time Example step by step. I hope now you understood the need and use of the Adapter Design Pattern in Real-time Applications.

Leave a Reply

Your email address will not be published. Required fields are marked *