Home > Enterprise >  What is the difference between Adapter pattern vs dependency injection in OOP?
What is the difference between Adapter pattern vs dependency injection in OOP?

Time:03-11

I have been studying Software architectures and design and I am in the Adapter pattern implementation part. This pattern looks similarly to the dependency injection that most framework uses such as Symfony, Angular, Vue, React, etc. What are their differences?

Or is it the frameworks implementation of Adapter pattern?

CodePudding user response:

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
  • Related