4

I'm reading about the Factory Method pattern.

I can understand when there is a single factory class, i.e. StoreFactory#getStore(), that returns a Store implementation based on some runtime or other state.

But, from reading (e.g. this link), there seems to be a general pattern of people creating an abstract factory class, to which other factory classes extend:

 public abstract class AbstractFactory { public abstract Store getStore(int store); } public class StoreFactoryA extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplA(); } if(Store == 2) { return new MyStoreImplB(); } } } public class StoreFactoryB extends AbstractFactory { public Store getStore(int Store) { if(Store == 1) { return new MyStoreImplC(); } if(Store == 2) { return new MyStoreImplD(); } } } public class Runner { public static void main(String[] args) { AbstractFactory storeFactory = new StoreFactoryA(); Store myStore = storeFactory.getStore(1); } } 

My example is contrived, but models that of the aforementioned link.

This implementation seems kind of chicken-egg to me. Use the Factory Method pattern to eliminate the need for the client code to specify a class type, yet now the client code needs to selectively choose the correct factory to use, i.e. StoreFactoryA, StoreFactoryB?

What is the reasoning behind using the abstract class here?

4
  • 4
    Your example is a little too simplistic. The point of an abstract factory is so that you can a) choose an entire factory at runtime, and then b) make that factory produce a whole collection of objects. The logical grouping is what's important. This prevents you from mixing two object types that should belong to different factories and can't ever practically occur together. Commented Sep 6, 2012 at 5:26
  • @KerrekSB: This should be an answer. Commented Sep 6, 2012 at 6:01
  • @casablanca: Thanks, though I think Sameer's answer is essentially saying the same and does a fine job. Commented Sep 6, 2012 at 7:13
  • @KerrekSB: Ah, looks like he posted just after my comment. :) Commented Sep 6, 2012 at 15:38

4 Answers 4

4

The link you are reading unfortunately does not give a realistic example of the pattern. In fact, as per the original GoF Design Patterns, this pattern is called Abstract Factory (Factory method is a different pattern).

Abstract Factory pattern is used when you have factories that can create a family of objects. e.g. You can have and AbstractGUIFactory which can have methods createButton(), createWindow(), createTitleBar etc. And then, you would have concrete factories like WindowsGUIFactory, MacGUIFactory, MotifGUIFactory etc, each of which will produce Button, Window, TitleBar objects in their own way.

The factory will be set to one implementation at some point in the application (probably using configuration) and then that factory will be used wherever the objects need to be created.

If you are learning Design Patterns, the best advice is to start with the classic GoF book.

Sign up to request clarification or add additional context in comments.

1 Comment

Lots of conflicting material out there... makes it hard to understand what is what. In the back of my mind (having used the DAO pattern), I thought as much.
0

The pattern gets interesting especially when it comes to testing. Now you can inject an AbstractFactory into a class and choose different types for different environments or tests without the need to change the classes (if the factory creates types that share common interfaces).

The using classes don't have to depend on the concrete implementation of a factory and don't have to depend on the real implementations of the created types.

Comments

0

When using Factory method, the method is still in the object, and you need to instantiate the correct object to access the method. The only abstraction you achieve, is because of the Abstract class.

The Factory Method pattern is designed in this way. Perhaps, you were expecting it to be something like the Abstract Factory pattern, which has a better abstraction than the Factory Method.

http://c2.com/cgi/wiki?AbstractFactoryVsFactoryMethod

Comments

0

You gain extensibility and decoupling through inversion of control, while still allowing the object a margin of control over the actual process and timing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.