factory-pattern

What is the difference between Builder Design pattern and Factory Design pattern?

混江龙づ霸主 提交于 2019-11-26 04:57:01
问题 What is the difference between the Builder design pattern and the Factory design pattern? Which one is more advantageous and why ? How do I represent my findings as a graph if I want to test and compare/contrast these patterns ? 回答1: With design patterns, there usually is no "more advantageous" solution that works for all cases. It depends on what you need to implement. From Wikipedia: Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of

“Downcasting” unique_ptr<Base> to unique_ptr<Derived>

别说谁变了你拦得住时间么 提交于 2019-11-26 03:38:02
问题 I have a series of factories that return unique_ptr<Base> . Under the hood, though, they are providing pointers to various derived types, i.e unique_ptr<Derived> , unique_ptr<DerivedA> , unique_ptr<DerivedB> etc. Given DerivedA : Derived and Derived : Base we\'d have: unique_ptr<Base> DerivedAFactory() { return unique_ptr<Base>(new DerivedA); } What I need to do is to \"cast\" the pointer from the returned unique_ptr<Base> to some derived level (not necessarily the original internal one). To

Why does Hibernate require no argument constructor?

99封情书 提交于 2019-11-26 03:05:29
问题 The no-argument constructor is a requirement (tools like Hibernate use reflection on this constructor to instantiate objects). I got this hand-wavy answer but could somebody explain further? Thanks 回答1: Hibernate, and code in general that creates objects via reflection use Class<T>.newInstance() to create a new instance of your classes. This method requires a public no-arg constructor to be able to instantiate the object. For most use cases, providing a no-arg constructor is not a problem.

Factory Pattern. When to use factory methods?

旧街凉风 提交于 2019-11-26 01:09:57
问题 When is it a good idea to use factory methods within an object instead of a Factory class? 回答1: I like thinking about design pattens in terms of my classes being 'people,' and the patterns are the ways that the people talk to each other. So, to me the factory pattern is like a hiring agency. You've got someone that will need a variable number of workers. This person may know some info they need in the people they hire, but that's it. So, when they need a new employee, they call the hiring

Factory method with DI and IoC

百般思念 提交于 2019-11-26 00:19:28
问题 I am familiar with these patterns but still don\'t know how to handle following situation: 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; } } } In general the problem is with amount of references that needs to be injected. It will be even worse when there are more cars. First approach that comes to my mind is to inject

How do I pass values to the constructor on my wcf service?

一笑奈何 提交于 2019-11-25 23:34:04
问题 I would like to pass values into the constructor on the class that implements my service. However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor. I would like to be able to pass in a factory that creates my service object. What I have found so far: WCF Dependency Injection Behavior which is more than what I am looking for and seems to be over-complex for my needs. 回答1: You'll need to implement a combination of custom