Factory Pattern without a Switch or If/Then

How about this (no Dictionary required and note that you will get an syntax error if your try to Create<Position>()): EDIT – Updated to use an IPosition interface implemented explicitly. Only instances of IPosition can access the member functions (e.g. <implementation of Manager>.Title will not compile). EDIT #2 Factory.Create should return an IPosition not T … Read more

Is it possible to use a c# object initializer with a factory method?

No. Alternatively you could accept a lambda as an argument, which also gives you full control in which part of the “creation” process will be called. This way you can call it like: MyClass instance = MyClass.FactoryCreate(c=> { c.SomeProperty = something; c.AnotherProperty = somethingElse; }); The create would look similar to: public static MyClass FactoryCreate(Action<MyClass> … Read more

What is the difference in case of intent and application between these two Patterns? [closed]

With the Factory pattern, you produce instances of implementations (Apple, Banana, Cherry, etc.) of a particular interface — say, IFruit. With the Abstract Factory pattern, you provide a way for anyone to provide their own factory. This allows your warehouse to be either an IFruitFactory or an IJuiceFactory, without requiring your warehouse to know anything … Read more

Returning Objects in C++

Depending on your usage, there are a couple of options you could go with here: Make a copy every time you create an animal: class AnimalLister { public: Animal getNewAnimal() { return Animal(); } }; int main() { AnimalLister al; Animal a1 = al.getNewAnimal(); Animal a2 = al.getNewAnimal(); } Pros: Easy to understand. Requires no … Read more

java.lang.IllegalStateException:Could not find backup for factory javax.faces.application.ApplicationFactory

That may happen if your webapp’s runtime classpath is polluted with multiple JSF impls/versions. The org.apache.myfaces entries in the stack trace tells that you’re using MyFaces. This problem thus suggests that you’ve another JSF implementation like Mojarra in the webapp’s runtime classpath which is conflicting with it. It’s recognizable by jsf-api.jar, or jsf-impl.jar, or javax.faces.jar. … Read more

Unit testing factory methods which have a concrete class as a return type

Often, there’s nothing wrong with creating public properties that can be used for state-based testing. Yes: It’s code you created to enable a test scenario, but does it hurt your API? Is it conceivable that other clients would find the same property useful later on? There’s a fine line between test-specific code and Test-Driven Design. … Read more

Factory Pattern – CreateInstance static or not?

I’m very hesitant to categorize “instance versus static” as a matter of taste. This sort of implies that it’s aesthetic like a favorite color or, more appropos, camelCase versus PascalCase. Instance versus static is more a question of tradeoffs. With instance members of any kind, you get all of the benefits of polymorphism, since you … Read more

“Singleton” factories, ok or bad?

It really depends on what you’re doing and the scope of your application. If it’s just a fairly small app and it’s never going to grow beyond this, then your current approach may well be fine. There is no universal “best” practice for these things. While I wouldn’t recommend using singletons for anything other than … Read more