1

I am trying to understand the factory design pattern better.

I understand that the pattern usually includes the following (is this always the case?):

1. Product
2. Concrete Product
3. Creator
4. Concrete Creator

From what I understand, the point of the factory method is that the Creator might look like

Public abstract class Creator
    public void doSomething(){
        Product product= createProduct();
        product.doSomethingElse();
    }

    public abstract Product createProduct();
}

where createProduct() is the factoryMethod.

Then when the Concrete Creator is created, we override Creator.createProduct().

If this factory method is only used for creating a product, does that mean we can technically not have a factory method, but still follow the Factory pattern?

For example, instead of

Public ConcreteCreator extends Creator{
  public Product createProduct(){
     return new ConcreteProduct()
  }
}

we do something like

Public ConcreteCreator extends Creator{
  Product product;
  public ConcreteCreator(){
    this.product = new ConcreteProduct;
  }
}

and change the Creator to something like

Public abstract class Creator
    Product product;
    public void doSomething(){
        this.product.doSomethingElse();
    }
}

It seems like this is basically doing the same thing as the factory pattern, where the concreteproduct still responsible for creating its own ConcreteProducts, except it does it in the constructor instead of overriding the factory methods.

Is this only not done because it's cleaner to move the creation logic of the ConcreteProducts out of the constructor into a factory method (i.e. only create concrete products as needed etc)? Or is there some case where we wouldn't be able to maintain the same behaviour if we only created the ConcreteProducts in the constructor of the ConcreteCreator?

user3504410
  • 173
  • 1
  • 13
  • 2
    A factory might make only one _kind_ of thing, but it doesn't make only one thing! – Matt Timmermans Nov 11 '19 at 16:02
  • If you're concerned with `ProductFactory.create()` vs `new Product()`, then this is already well answered: https://stackoverflow.com/a/629216/2957169. However I think you are actually asking if you can construct the object in your factory's constructor; Matt beat me to it! This will always return the same object. I would expect most factories to return a new instance each time (though in some cases it may be a desireable implementation, for example for immutable classes you might return a singleton instance). I think your question title is very misleading. – charles-allen Nov 11 '19 at 16:04
  • @AjahnCharles I agree, I have updated the title now. But Matt does bring up a point that I didnt' consider. I was probably reading too deeply into simple examples I found online, and didn't consider how the pattern would work for more practical cases – user3504410 Nov 11 '19 at 16:35

3 Answers3

1

I think the problem that confuses you is that you shouldn't have a doSomething() method in the Creator class. The factory class should be responsible for creating the product, and nothing else. In other words a factory class doesn't use its own product. Therefore your Creator class is not an abstract factory.

erosb
  • 2,943
  • 15
  • 22
  • I see many examples online that seem to contradict this, ie: https://dzone.com/articles/java-the-factory-pattern In this case the Creator is `Encryptor`, and `Encryptor` has a method `writeToDisk` that calls the factory method `getEncryptionAlgorithm` – user3504410 Nov 11 '19 at 16:27
0

One example where the constructor won't be able to do the exact same thing as a factory pattern is when you need to share the objects in some way, like in an object pool or a cache.

Another example would be if you want the creator to be configurable, so that, with basis on some configuration, one concrete factory would be used in place of the other, without changing the code in any way.

Haroldo_OK
  • 6,612
  • 3
  • 43
  • 80
0

Maybe I'm not understanding well but if I understood the core of your question is why to use a method and not a constructor to return the concrete factory object.

I think that it will always depends of case to case but, methods are a more flexible than constructors. Constructors goal is construct the object, and should be restricted to that.

For instance, if for any reason you need business logic being used before returning the object,synchronization or verification, for me doesn't make too much sense use the constructor (constructors should be used the construct objects and not make operations, that is reserved to methods).

tagus
  • 129
  • 1
  • 5