设计模式中的那些工厂 (2)

依赖注入可以使得我们的代码变得更加良好,扩展性更强。

// 依赖注入 var builder = new ContainerBuilder(); builder.RegisterType<VolunteerFactory>().As<ILeifengFactory>(); builder.RegisterType<SqlServerFactory>().As<IDbFactory>(); var container = builder.Build(); var leifengFactory = container.Resolve<ILeifengFactory>(); var volunteer = leifengFactory.CreateLeifeng(); volunteer.Wash(); var dbFactory = container.Resolve<IDbFactory>(); dbFactory.CreateDepartmentRepo().CreateDepartment(null); Reference

https://github.com/WeihanLi/DesignPatterns/blob/master/CreatePattern/SimpleFactoryPattern

https://github.com/WeihanLi/DesignPatterns/tree/master/CreatePattern/AbstractFactoryPattern

https://github.com/WeihanLi/DesignPatterns/blob/master/CreatePattern/FactoryMethodPattern

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyjjws.html