What are the differences between Abstract Factory and Factory design patterns?

后端 未结 17 1138
醉话见心
醉话见心 2020-11-22 01:57

I know there are many posts out there about the differences between these two patterns, but there are a few things that I cannot find.

From what I have been reading,

相关标签:
17条回答
  • 2020-11-22 02:31

    Abstract Factory is an interface for creating related products, but Factory Method is only one method. Abstract Factory can be implemented by multiple Factory Methods.

    Abstract Factory UML

    0 讨论(0)
  • 2020-11-22 02:32

    Let us put it clear that most of the time in production code, we use abstract factory pattern because class A is programmed with interface B. And A needs to create instances of B. So A has to have a factory object to produce instances of B. So A is not dependent on any concrete instance of B. Hope it helps.

    0 讨论(0)
  • 2020-11-22 02:34
    1. My first question is about the abstract factory. Is its role to allow you to create families of concrete objects in (that can depend on what specific factory you use) rather than just a single concrete object?

    Yes. The intent of Abstract Factory is:

    Provide an interface for creating families of related or dependent objects without specifying their concrete classes.


    1. Does the abstract factory only return one very large object or many objects depending on what methods you call?

    Ideally it should return one object per the method client is invoking.

    1. My understanding is that the factory method pattern has a Creator interface that will make the ConcreteCreator be in charge of knowing which ConcreteProduct to instantiate. Is this what it means by using inheritance to handle object instantiation?

    Yes. Factory method uses inheritance.

    1. Abstract Factory pattern delegate the responsibility of object instantiation to another object via composition? What does this mean?

    AbstractFactory defines a FactoryMethod and ConcreteFactory is responsible for building a ConcreteProduct. Just follow through the code example in this article.

    You can find more details in related SE posts:

    What is the basic difference between the Factory and Abstract Factory Patterns?

    Design Patterns: Factory vs Factory method vs Abstract Factory

    0 讨论(0)
  • 2020-11-22 02:35

    Difference between AbstractFactory and Factory design patterns are as follows:

    • Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.
    • Factory Method pattern exposes a method to the client for creating the object whereas in the case of Abstract Factory they expose a family of related objects which may consist of these Factory methods.
    • Factory Method pattern hides the construction of a single object whereas Abstract Factory hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.
    • Abstract Factory pattern uses composition to delegate the responsibility of creating an object to another class while Factory Method design pattern uses inheritance and relies on a derived class or subclass to create an object.
    • The idea behind the Factory Method pattern is that it allows for the case where a client doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.!

    Factory Method Pattern Implementation: Factory Method UML

    Abstract Factory Pattern Implementation:

    Abstract Factory UML

    0 讨论(0)
  • 2020-11-22 02:36

    Abstract factory creates a base class with abstract methods defining methods for the objects that should be created. Each factory class which derives the base class can create their own implementation of each object type.

    enter image description here

    Factory method is just a simple method used to create objects in a class. It's usually added in the aggregate root (The Order class has a method called CreateOrderLine)

    enter image description here

    Abstract factory

    In the example below we design an interface so that we can decouple queue creation from a messaging system and can therefore create implementations for different queue systems without having to change the code base.

    interface IMessageQueueFactory
    {
      IMessageQueue CreateOutboundQueue(string name);
      IMessageQueue CreateReplyQueue(string name);
    }
    
    public class AzureServiceBusQueueFactory : IMessageQueueFactory
    {
          IMessageQueue CreateOutboundQueue(string name)
          {
               //init queue
               return new AzureMessageQueue(/*....*/);
          }
    
          IMessageQueue CreateReplyQueue(string name)
          {
               //init response queue
               return new AzureResponseMessageQueue(/*....*/);
          }
    
    }
    
    public class MsmqFactory : IMessageQueueFactory
    {
          IMessageQueue CreateOutboundQueue(string name)
          {
               //init queue
               return new MsmqMessageQueue(/*....*/);
          }
    
          IMessageQueue CreateReplyQueue(string name)
          {
               //init response queue
               return new MsmqResponseMessageQueue(/*....*/);
          }
    }
    

    Factory method

    The problem in HTTP servers is that we always need an response for every request.

    public interface IHttpRequest
    {
        // .. all other methods ..
    
        IHttpResponse CreateResponse(int httpStatusCode);
    }
    

    Without the factory method, the HTTP server users (i.e. programmers) would be forced to use implementation specific classes which defeat the purpose of the IHttpRequest interface.

    Therefore we introduce the factory method so that the creation of the response class also is abstracted away.

    Summary

    The difference is that the intended purpose of the class containing a factory method is not to create objects, while an abstract factory should only be used to create objects.

    One should take care when using factory methods since it's easy to break the LSP (Liskov Substitution principle) when creating objects.

    0 讨论(0)
  • 2020-11-22 02:36

    I would favor Abstract Factory over Factory Method anytime. From Tom Dalling's example (great explanation btw) above, we can see that Abstract Factory is more composable in that all we need to do is passing a different Factory to the constructor (constructor dependency injection in use here). But Factory Method requires us to introduce a new class (more things to manage) and use subclassing. Always prefer composition over inheritance.

    0 讨论(0)
提交回复
热议问题