Dependency injection 依赖注入与工厂模式

Dependency injection 依赖注入与工厂模式,dependency-injection,factory-pattern,design-patterns,Dependency Injection,Factory Pattern,Design Patterns,引用的大多数使用依赖注入的示例,我们也可以使用工厂模式来解决。看起来在使用/设计方面,依赖注入和工厂之间的区别是模糊的或很小的 有一次,有人告诉我,你是如何使用它的,这才有意义 我曾经使用DI容器来解决一个问题,后来我重新设计了它,使其与一个简单的工厂一起工作,并删除了对StructureMap的引用 谁能告诉我它们之间的区别是什么,在哪里使用什么,这里的最佳实践是什么?我建议保持概念的简单明了。依赖注入更多地是松散耦合软件组件的一种体系结构模式。工厂模式只是将创建其他类的对象的责任与另一个实体

引用的大多数使用依赖注入的示例,我们也可以使用工厂模式来解决。看起来在使用/设计方面,依赖注入和工厂之间的区别是模糊的或很小的

有一次,有人告诉我,你是如何使用它的,这才有意义

我曾经使用DI容器来解决一个问题,后来我重新设计了它,使其与一个简单的工厂一起工作,并删除了对StructureMap的引用


谁能告诉我它们之间的区别是什么,在哪里使用什么,这里的最佳实践是什么?

我建议保持概念的简单明了。依赖注入更多地是松散耦合软件组件的一种体系结构模式。工厂模式只是将创建其他类的对象的责任与另一个实体分离的一种方法。工厂模式可以被称为实现DI的工具。依赖项注入可以通过多种方式实现,如使用构造函数的DI、使用映射xml文件等。

使用依赖项注入,客户机不需要自己获取依赖项,所有这些都是事先准备好的

对于工厂,必须有人打电话给它们,将生成的对象送到需要它们的地方

区别主要在于这一行调用工厂并获取构造的对象


但是对于工厂,你必须在任何需要这样一个对象的地方写这一行。使用DI,您只需创建一次连接(使用和创建的对象之间的关系),然后依赖对象的存在。另一方面,DI通常需要更多的准备工作(多少取决于框架)。我相信DI是工厂中的一种抽象层,但它们也提供了抽象之外的好处。真正的工厂知道如何实例化单个类型并对其进行配置。好的DI层通过配置提供实例化和配置许多类型的能力

显然,对于一些简单类型的项目,在其构建中需要相对稳定的业务逻辑,工厂模式很容易理解、实现,并且运行良好


OTOH,如果您有一个包含许多类型的项目,您希望经常更改这些类型的实现,那么DI通过其配置为您提供了在运行时执行此操作的灵活性,而无需重新编译工厂。

使用工厂时,您的代码实际上仍然负责创建对象。通过DI,您可以将该职责外包给另一个类或框架,这与您的代码是分开的。

有一些问题很容易用依赖项注入解决,但用一套工厂解决不了

一方面,控制反转和依赖注入(IOC/DI)与另一方面,服务定位器或工厂套件(工厂)之间的一些区别是:

IOC/DI本身就是一个完整的域对象和服务生态系统。它以您指定的方式为您设置一切。您的域对象和服务是由容器构造的,而不是自己构造的:因此它们对容器或工厂没有任何依赖关系。IOC/DI允许极高程度的可配置性,所有配置都在应用程序最顶层(GUI、Web前端)的单个位置(容器的构造)


Factory抽象掉了一些域对象和服务的构造。但是域对象和服务仍然负责弄清楚如何构造它们自己以及如何获得它们所依赖的所有东西。所有这些“活动”依赖项都会过滤应用程序中的所有层。没有一个地方可以配置所有东西。

我相信DI是一种配置或实例化bean的方法。DI可以通过许多方式完成,比如构造函数、setter-getter等

工厂模式只是实例化bean的另一种方式。此模式主要用于必须使用factory设计模式创建对象时,因为在使用此模式时,您不配置bean的属性,只实例化对象

检查此链接:

Binoj

我认为你不必选择其中一个

将依赖类或接口移动到类构造函数或setter的行为遵循DI模式。传递给构造函数或集合的对象可以用Factory实现

何时使用?使用开发人员驾驶室中的一个或多个模式。他们觉得最舒服、最容易理解的是什么。

我的想法:

依赖注入:将协作者作为参数传递给构造函数。 依赖注入框架:一个通用的、可配置的工厂,用于创建要作为参数传递给构造函数的对象

除了实例化和注入之外,生命周期管理是依赖容器承担的职责之一。容器有时在实例化后保留对组件的引用,这是它被称为“容器”而不是工厂的原因。依赖项注入容器通常只保留对它需要管理生命周期的对象的引用,或者对将来注入重用的对象的引用,如单例或flyweights。当配置为为为每个容器调用创建某些组件的新实例时,容器通常只会忘记创建的对象


发件人:

我一读到关于DI的文章就有了同样的问题,最后来到了这篇文章。 最后,这就是我所理解的,但如果我错了,请纠正我

“很久以前,有一些小王国,它们有自己的管理机构,根据自己的书面规则进行控制和决策。后来形成了
MyBean mb = ctx.getBean("myBean");
MyBean mb = MyBeanForEntreprise1(); //In the classes of the first enterprise
MyBean mb = MyBeanForEntreprise2(); //In the classes of the second enterprise
@Autowired MyBean mbForEnterprise1; //In the classes of the first enterprise
@Autowired MyBean mbForEnterprise2; //In the classes of the second enterprise
MyBean mb = (MyBean)MyFactory.get("myBeanForEntreprise1"); //In the classes of the first enterprise
MyBean mb = (MyBean)MyFactory.get("myBeanForEntreprise2"); //In the classes of the second enterprise
MyBean mb = (MyBean)MyFactory.get("mb"); 
class Car
{
    private Engine engine;
    private SteeringWheel wheel;
    private Tires tires;

    public Car(Engine engine, SteeringWheel wheel, Tires tires)
    {
        this.engine = engine;
        this.wheel = wheel;
        this.tires = tires;
    }
}
static class CarFactory
{
    public ICar BuildCar()
    {
        Engine engine = new Engine();
        SteeringWheel steeringWheel = new SteeringWheel();
        Tires tires = new Tires();
        ICar car = new RaceCar(engine, steeringWheel, tires);
        return car;
    }   
}
static void Main()
{
     ICar car = CarFactory.BuildCar();
     // use car
}
void RaceCar() // example #1
{
    ICar car = CarFactory.BuildCar();
    car.Race();
}

void RaceCar(ICarFactory carFactory) // example #2
{
    ICar car = carFactory.BuildCar();
    car.Race();
}

void RaceCar(ICar car) // example #3
{
    car.Race();
}
public class MyClass{

  MyInterface find= null;

  //Constructor- During the object instantiation

  public MyClass(MyInterface myInterface ) {

       find = myInterface ;
  }

  public void myMethod(){

       find.doSomething();

  }
}
IWebClient client = factoryWithCache.GetWebClient(url: "stackoverflow.com",
        useCookies: false, connectionTimeout: 120);
public AddressModelFactory(IAddressAttributeService addressAttributeService,
        IAddressAttributeParser addressAttributeParser,
        ILocalizationService localizationService,
        IStateProvinceService stateProvinceService,
        IAddressAttributeFormatter addressAttributeFormatter)
    {
        this._addressAttributeService = addressAttributeService;
        this._addressAttributeParser = addressAttributeParser;
        this._localizationService = localizationService;
        this._stateProvinceService = stateProvinceService;
        this._addressAttributeFormatter = addressAttributeFormatter;
    }
 public CustomerController(IAddressModelFactory addressModelFactory,
        ICustomerModelFactory customerModelFactory,
        IAuthenticationService authenticationService,
        DateTimeSettings dateTimeSettings,
        TaxSettings taxSettings,
        ILocalizationService localizationService,
        IWorkContext workContext,
        IStoreContext storeContext,
        ICustomerService customerService,
        ICustomerAttributeParser customerAttributeParser,
        ICustomerAttributeService customerAttributeService,
        IGenericAttributeService genericAttributeService,
        ICustomerRegistrationService customerRegistrationService,
        ITaxService taxService,
        CustomerSettings customerSettings,
        AddressSettings addressSettings,...
@Component
class Parent {
    // ...
    @Autowired
    Parent(Dep1 dep1, Dep2 dep2, ..., DepN depN) {
        this.dep1 = dep1;
        this.dep2 = dep2;
    }

    void method(int p) {
        Child c = new Child(dep1, dep2, ..., depN, p);
        // ...
    }
}
@Component
class Parent {
    // ...
    @Autowired
    Parent(ChildFactory childFactory) {
        this.childFactory = childFactory;
    }

    void method(int p) {
        Child c = childFactory.newChild(p);
        // ...
    }
}

@Component
class ChildFactory {
    // ...
    @Autowired
    Parent(Dep1 dep1, Dep2 dep2, ..., DepN depN) {
        this.dep1 = dep1;
        this.dep2 = dep2;
        // ...
        this.depN = depN;
    }

    Child newChild(int p) {
        return new Child(dep1, dep2, ..., depN, p);
    }
}
ICarBusiness carBusiness = BusinessFactory.CreateCarBusiness();
public static class BusinessFactory
{
    public static ICarBusiness CreateCarBusiness()
    {
       return Container.Resolve<ICarBusiness>();
    }
}