OOP 中的适配器模式与依赖注入有什么区别?

发布于 2025-01-12 21:33:51 字数 144 浏览 3 评论 0原文

我一直在大学里学习软件架构和设计,并且在设计模式部分学习。我注意到适配器模式实现看起来类似于大多数框架使用的依赖注入,例如 Symfony、Angular、Vue、React,我们导入一个类并在构造函数中类型提示它。

它们有什么区别或者是适配器模式的框架实现?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2025-01-19 21:33:51

依赖注入可以用在适配器模式中。那么让我们一步一步来。让我展示什么是适配器模式和依赖注入。

正如 Wiki 关于适配器模式所述:

在软件工程中,适配器模式是一种软件设计
模式(也称为包装器,与
装饰器模式)允许现有类的接口
用作另一个接口。它经常被用来制作现有的
类可以与其他类一起使用,而无需修改其源代码。

让我们看一个现实生活中的例子。例如,我们有一个开车旅行的旅行者。
但有时有些地方他不能开车去。例如,他不能在森林里开车。但他可以在森林里骑马。但是,Traveller 类没有办法使用Horse 类。所以,这是一个可以使用模式Adapter的地方。

让我们看看 VehicleTourist 类的样子:

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();
    }
}

以及动物抽象及其实现:

public interface IAnimal
{
    void Move();
}

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

这是从 Horse的适配器类Vehicle

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

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

我们可以像这样运行我们的代码:

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);
}   

但是依赖注入是提供对象需要的对象(其依赖项)而不是让它自己构建它们

所以我们例子中的依赖是:

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

注入是:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文