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

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

“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

When to use Factory method pattern?

Use a factory method (not abstract factory) when you want to reuse common functionality with different components. Example: Imagine you have an M16 rifle. Something like this: public class M16 { private Scope scope = new StandardScope(); private SecondaryWeapon secondary = new Bayonet(); private Camouflage camo = new DesertCamo(); public double getMass() { // Add … Read more

Populating an association with children in factory_girl

The Factory.after_ hooks appear to be the only way to do this successfully. I’ve figured out a way to maintain the build strategy without duplicating code: Factory.define :foo do |f| f.name “A Foo” f.after(:build) { |foo| foo.bars << Factory.build(:bar, :foo => foo) } f.after(:create) { |foo| foo.bars.each { |bar| bar.save! } } end The documentation … Read more

Factory, Abstract Factory and Factory Method

The Gang Of Four “Design Patterns; Elements of Reusable Object-Oriented Software” book contains two entries, “Abstract Factory” (aka ‘Virtual Constructor’) and “Factory Method”. I don’t know about “Concrete Factory.” I’ve heard the term, but never given it too much thought. Factory Method In “Factory Method” an object has a method which is responsible for the … Read more

What exactly is a Class Factory?

Here’s some supplemental information that may help better understand several of the other shorter, although technically correct, answers. In the strictest sense a Class Factory is a function or method that creates or selects a class and returns it, based on some condition determined from input parameters or global context. This is required when the … Read more

How to avoid ‘instanceof’ when implementing factory design pattern?

You could implement the Visitor pattern. Detailed Answer The idea is to use polymorphism to perform the type-checking. Each subclass overrides the accept(Visitor) method, which should be declared in the superclass. When we have a situation like: void add(Vehicle vehicle) { //what type is vehicle?? } We can pass an object into a method declared … Read more