Moving on to the concept of Design Patterns, let's explain the Adapter Design Pattern. This pattern is part of the Structural Design Patterns.
What is Adapter Design Pattern?
According to the GoF's definition, the intent of this pattern is to:
"Convert the interface of a class into another interface that clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces."
To understand this definition, let's use a real-world simple example. You know a language, "A". You visit a place where language "B" is spoken. But you don't know how to speak that language. So you meet a person who knows both languages, in other words A and B. So he can act as an adapter for you to talk with the local people of that place and help you communicate with them.
Another example is a kind of situation where the client hires someone to perform a task for him, that he cannot do directly without a third person. This means that the client needs an intermediary to co-ordinate with the third person and get the task done for him. This is what the role of an adapter is. We will be using this example to explain this pattern.
To start with, this pattern can be implemented in one of two ways (or 2 types you can say). We will be explaining both of them along with a real-world example for a better understanding. But before that, we will define the components of our system. They include:
- Adaptee: Defines an existing interface that needs adapting; it represents the component with which the client wants to interact with the.
- Target: Defines the domain-specific interface that the client uses; it basically represents the interface of the adapter that helps the client interact with the adaptee.
- Adapter: Adapts the interface Adaptee to the Target interface; in other words, it implements the Target interface, defined above and connects the adaptee, with the client, using the target interface implementation
- Client: The main client that wants to get the operation, is done from the Adaptee.
Now moving on to the types and their implementations.
Class Adapter
A real-world example
You work on a client's projects. You have a team of two developers, A & B (specialist in ASP.NET), who work on this client's project directly, without your involvement. This is important to note here, in other words without your involvement. We will get to why it is so.
So technically, your code will be like the following code:
Everything is fine so far. So your client has a new assignment and needs a Silverlight technology to work on. You don't have the team for this technology and don't want to lose the opportunity to work on it So what do you? You create a new team and add members to it.
Since it is a new technology and you have a good hand on Silverlight, you decide to monitor the things yourself and get the task done. So this is where an adapter is useful. You become an adapter and get the things done, by being an intermediate for the client and the new Silverlight team. You get the requirements from the client and pass them to the team and execute the project. So here our adapter is now represented by the ExecuteProject() method in the adapter class. See the code below:
This is why I said without your involvement, when I started with the concept. The main requirement that forced us to use an adapter is that the client is not compatible with the new team introduced by you. So you represent yourself to the client, on behalf of your team and get the project done. Here, you bring your team into the picture using the concept of inheritance and in our code, the representations are :


Join the conversation! Your thoughts help the community grow.