4

I have been studying Software architectures and design in my uni and I am in the design pattern section. I noticed that the adapter pattern implementation looks similarl to the dependency injection that most framework uses such as Symfony, Angular, Vue, React, that we import a class and type hint it in our constructor.

What are their differences or is it the frameworks implementation of Adapter pattern?

1
  • 1
    Please explain in detail what you think looks similar.
    – jaco0646
    Mar 9, 2022 at 14:52

1 Answer 1

7

Dependency injection can be used in adapter pattern. So let's go step by step. Let me show what adapter pattern and dependency injection are.

As Wiki says about adapter pattern:

In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.

Let's see a real life example. For example, we have a traveller who travels by car. But sometimes there are places where he cannot go by car. For example, he cannot go by car in forest. But he can go by horse in forest. However, class of Traveller does not have a way to use Horse class. So, it is a place where pattern Adapter can be used.

So let's look how Vehicle and Tourist class look like:

public interface IVehicle
{
    void Drive();
}

public class Car : IVehicle
{
    public void Drive()
    {
        Console.WriteLine("Tourist is going by car");
    }
}


public class Tourist
{
    public void Travel(IVehicle vehicle)
    {
        vehicle.Drive();
    }
}

and animal abstractions and its implementations:

public interface IAnimal
{
    void Move();
}

public class Horse : IAnimal
{
    public void Move()
    {
        Console.WriteLine("Horse is going");
    }
}

This is an adapter class from Horse to Vehicle:

public class HorseToVehicleAdapter : IVehicle
{
    Horse _horse;
    public HorseToVehicleAdapter(Horse horse)
    {
        _horse = horse;
    }

    public void Drive()
    {
        _horse.Move();
    }
}

We can run our code like this:

static void Main(string[] args)
{   
    Tourist tourist = new Tourist();
 
    Car auto = new Car();
 
    tourist.Travel(auto);
    // tourist in forest. So he needs to ride by horse to travel further
    Horse horse = new Horse();
    // using adapter
    IVehicle horseVehicle = new HorseToVehicleAdapter(horse);
    // now tourist travels in forest
    tourist.Travel(horseVehicle);
}   

But dependency injection is providing the objects that an object needs (its dependencies) instead of having it construct them itself.

So dependency in our example is:

public class Tourist
{
    public void Travel(IVehicle vehicle) // dependency
    {
        vehicle.Drive();
    }
}

And injection is:

IVehicle horseVehicle = new HorseToVehicleAdapter(horse);
// now tourist travels in forest
tourist.Travel(horseVehicle); // injection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.