views:

54

answers:

3

Hi,

There is both an abstract and factory pattern. What exactly is the difference between these and why one be used over another?

Thanks

+2  A: 

A good example is given in the wikipedia article: http://en.wikipedia.org/wiki/Abstract_factory_pattern

An abstract factory is a "factory interface" that can be implemented by various concrete factories.

Benoit Courtine
A: 

If you're looking for a good quick reference site for design patterns, check out http://www.dofactory.com/Patterns/Patterns.aspx for examples (using C#) and brief explanations.

Also, the Head First Design Patterns book (which uses Java examples) helps break down the different patterns in an easy to understand format.

I'd recommend those 2 resources for anyone just starting to research design patterns.

keith_c
A: 

There is both an abstract and factory pattern.

The Gang of Four book talks about two patterns: Factory Method, and Abstract Factory.

A Factory method is just what it sounds like, an abstract method used to create an object:

public interface SomeInterface {
    Foo create(); // factory method
}

public class SomeClass implements SomeInterface {
    Foo create() {
        // the concrete type being constructed is abstracted by the factory method
        return new DerivedFoo();
    }
}

An abstract factory is a type that defines an abstraction for creating objects. In the example above, SomeInterface is an abstract factory.

The two are not always coupled to each other. It's perfectly valid to have a regular concrete class with some functionality that also has a factory method. An abstract factory says that the purpose of the entire class is to create one or more objects.

munificent