DI和IoC的工厂方法
public class CarFactory{ public CarFactory(Dep1,Dep2,Dep3,Dep4,Dep5,Dep6) { } public ICar CreateCar(type) { switch(type) { case A: return new Car1(Dep1,Dep2,Dep3); break; case B: return new Car2(Dep4,Dep5,Dep6); break; } }}
public class CarFactory{ public CarFactory(IContainer container) { _container = container; } public ICar CreateCar(type) { switch(type) { case A: return _container.Resolve<ICar1>(); break; case B: return _container.Resolve<ICar2>(); break; } }}
public class CarFactory{ public CarFactory() { } public ICar CreateCar(type) { switch(type) { case A: return new Car1(new Dep1(),new Dep2(new Dep683(),new Dep684()),....) break; case B: return new Car2(new Dep4(),new Dep5(new Dep777(),new Dep684()),....) break; } }}
皈依舞