Abstract factory mode (Abstract factory)

  • Definition of factory mode:For the client, hiding object creation logic does not need to use new to generate objects.
  • Implementation mode of factory mode:
    • Create an interface such as Shape;
    • Create specific classes to implement Shape, such as circles, squares and triangles.
    • Create Shape’s Factory, and create different specific classes by Shape type.
    • Finally, the client first creates a Shape factory, and then passes the different shape type parameters to get the concrete class.

 

  • Abstract factory definition:Create super factories for other factories;
  • Abstract factory implementation:
  • Create an interface such as Shape and Color;
  • Create specific classes to implement the above respective interface methods;
  • Creating an abstract factory class can get Shape and Color objects;
  • Create Shape and Color factory classes and integrate abstract factory classes to produce concrete Shape and Color classes, such as circles, squares, and triangles.
  • Create a factory producer, which can choose the corresponding factory according to the parameters imported from outside.
  • Finally, we use the factory producer to get the corresponding factories and produce specific classes.

 

 

  • JDKExample:;
  • Project examples: each search domain is automatically created through factory mode.
  • Advantage:;
  • Disadvantages:;

Leave a Reply

Your email address will not be published. Required fields are marked *