2

I have read a lot of posts about different between Abstract Factory and Factory method, but there are a problem I can't understand.

One difference between the two is that with the Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation

Maybe I know why Abstract Factory pattern use composition and delegates to create object, but I can't understand why Factory Method pattern uses inheritance to create concrete class objects.


This question is not about what abstract factory is or what factory method is and hence does not have answer here

It is about why factory method seems to use inheritance when a client can call factory method directly too. Please unmark it as duplicate.

Number945
  • 4,631
  • 8
  • 45
  • 83
Rey
  • 83
  • 2
  • 9

1 Answers1

6

Abstract Factory

public interface IMyFactory
{
    IMyClass CreateMyClass(int someParameter);
}

Usage:

public class SomeOtherClass
{
    private readonly IMyFactory factory;

    public SomeOtherClass(IMyFactory factory)
    {
        this.factory = factory;
    }

    public void DoSomethingInteresting()
    {
        var mc = this.factory.CreateMyClass(42);
        // Do something interesting here
    }
}

Notice that SomeOtherClass relies on Composition to be composed with an IMyFactory instance.

Factory Method

public abstract class SomeOtherClassBase
{
    public void DoSomethingInteresting()
    {
        var mc = this.CreateMyClass(42);
        // Do something interesting here
    }

    protected abstract IMyClass CreateMyClass(int someParameter)
}

Usage:

public class SomeOtherClass2 : SomeOtherClassBase
{   
    protected override IMyClass CreateMyClass(int someParameter)
    {
        // Return an IMyClass instance from here
    }
}

Notice that this example relies on inheritance to work.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • ok, I understand but which style is better and what have advantages? Or we can use any of them? – grep Jun 19 '15 at 10:09
  • If you *favour composition over Inheritance* it should be clear which one you should favour ;) – Mark Seemann Jun 19 '15 at 10:16
  • 1
    @grep Abstract Factory typically is used with *families of products*, meaning the interface has one method for each product in the family. This example oversimplifies the pattern with a one-product family. Even though it uses composition, Abstract Factory is also risky because of inheritance. If you need to add (or remove) a product in the families, you've got to change the interface (add or remove a method) and therefore all the factories. Benefits of "composition over inheritance" are not really an obvious difference as stated. The main difference is whether you have a family of products. – Fuhrmanator Jul 26 '17 at 05:43
  • @MarkSeemann In your *factory method design*, you are assuming that the client will never call `CreateMyClass(int someParameter)` and will always call it through `DoSomethingInteresting()` indirectly. Why ? Because if it does call it directly, then the client also depended on object composition to get the created object. – Number945 Jul 20 '20 at 20:05
  • @Number945 `CreateMyClass` is `protected`, so clients can't call it directly. – Mark Seemann Jul 21 '20 at 06:47
  • @MarkSeemann Correct but GOF UML does not impose restriction that factory method in factory design pattern will always be protected. And if that be the case, we can make it public and clients can call it directly and hence, in such a case composition is used in factory design too ? – Number945 Jul 21 '20 at 09:55
  • @Number945 I don't have my GOF copy handy, but I believe you. In that case, you have a class with two responsibilities, and one of the is to create an instance of the class. I'm not sure I completely understand what you're asking about... – Mark Seemann Jul 21 '20 at 10:07