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

后端 未结 17 1174
醉话见心
醉话见心 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: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

提交回复
热议问题