Domain-Driven Design
> >
AbstractFactoryVsFactoryMethod

I've been using the factory pattern for a while, but don't know exactly if
I'm using the Abstract Factory or the Factory Method. They look the same to
me, but what are the differences?

PabloSchor



A Factory Method is a single method that instantiates some
implementation of an interface, usually depending on the input.

An Abstract Factory is a class with many creation methods. Its main
property is that the different objects it creates "go together", in that
if you changed the rules for which implementation to return from one
method, you will likely change which implementation to return from all
the methods.

Think of an Abstract Factory as One Big Factory Method that returns
multiple values. It just so happens that it is convenient to implement
it as an object with methods, each of which returns a different kind of
object.

DIFFERENCE #1: Scope. A Factory Method returns only one object, whereas
an Abstract Factory provides many methods so that it can create many
different, but related objects.

DIFFERENCE #2: Variability. An Abstract Factory is typically initialized
once for the application with the particular implementations of the
objects to return, then used; a Factory Method typically examines its
parameters to decide which implementation to return on a call-by-call
basis. This is merely a difference in the way the pattern is typically
implemented, and not part of what defines the pattern.

I hope this helps.

JBRainsberger


I would like to add one minor point to the explanation
above. There is a difference between a Factory and an
Abstract Factory.

A Factory class usually creates families of objects.

An Abstract Factory is the base for several different
Factory classes to build on. It's useful if you want
to build different factories.

For example, take a AbstractGarden class as an
Abstract Factory. AbstractGarden would then be the
base class for several different Gardens:
VeggieFactory, AnnualFactory and PerennialFactory.

Each one of those Factories would then know how to
create it's own family of plants.

( Example above taken from C# Design Patterns )

Minor distinction, but I think it's an important one.

Just my $0.02.

GriffinCaprio


>I've been using the factory pattern for a while, but don't know exactly if
>I'm using the Abstract Factory or the Factory Method. They look the same to
>me, but what are the differences?

One difference is use. You typically use Factory Method as part of a
Template Method. Something like:


public abstract class Application {
public void startup {
SplashScreen splashScreen = createSplashScreen()// Factory Method
splashScreen.show();
doStartupTasks();
splashScreen.hide();
runApplication();
}

public abstract SplashScreen createSplashScreen ()// Factory Method
}

public class JohnsApplication extends Application {
public SplashScreen createSplashScreen () { // Factory Method
SplashScreen splashScreen = new JohnsSplashScreen();
return splashScreen;
}
}


(Note that (at least according to Joshua Kerievsky) Factory Methods
are always polymorphic. If you don't have at least the possibility
of overriding the method to create something differently, it's not a
Factory Method. See Joshua's upcoming book "Refactoring to Patterns"
at:

http://www.industriallogic.com/xp/refactoring/

for more info.)

An Abstract Factory on the other hand, is used to create families of
objects. So you might have something like:


public abstract class WindowManager {
private static WindowManager currentWindowManager();
public static WindowManager getCurrentWindowManager() {
return currentWindowManaager();
}

public abstract Window createWindow();
public abstract Button createButton();
public abstract TextField createTextField();
}


Then you'd have concrete subclasses of WindowManager for each window
system. For example:


public class MacWindowManager extends WindowManager {
public Window createWindow() { return new MacWindow()}
public Button createButton() { return new MacButton()}
public TextField createTextField() { return new MacTextField()}
}

public class WindowsWindowManager extends WindowManager {
public Window createWindow() { return new WindowsWindow()}
public Button createButton() { return new WindowsButton()}
public TextField createTextField() { return new WindowsTextField()}
}


In your client code, to create a new window, you do something like:

Window window = WindowManager.getCurrentWindowManager.createWindow();

Note that WindowManager also uses the singleton pattern. Combining
Abstract Factory and Singleton is fairly common, since you often only
want one concrete factory in use at a time, and you want to insulate
client code from having to know which one is active.


JohnBrewer



AbstractFactoryVsFactoryMethod is mentioned on: ThreadView


VeryQuickWiki Version 2.6.3 - HTML Export