Home » Java » Creation mode (1) ABSTRACT FACTORY (Abstract Facto...

Creation mode (1) ABSTRACT FACTORY (Abstract Factory) - object creation model

1. intention

provides an interface to create a series of related or interdependent objects without specifying their specific classes,

2. alias


3. motivation

my factory needs to produce products that are a series of objects, rather than a single object that requires an abstract factory to solve.

Let's start with two concepts,

1) product hierarchical structure: inheritance structure of products, such as an abstract class is a subclass of the TV, Haier TV, Hisense TV, TCL TV, a product level structure between the abstract and concrete TV brand TV, TV is the abstract parent class, but the specific brand of TV is the son class
2) product family: in the abstract factory pattern, product family refers to the production by the same factory, is located in a different set of products in grade structure of products, such as Haier electric appliances factory Haier TV, Haier refrigerator, Haier TV in the TV product hierarchy, Haier refrigerator in refrigerator product grade structure.

The product family and product hierarchy are as follows:

to describe the picture here to write
Take an example,

to describe the picture here to write
Abstract factories are manufactured for a product family,

4. applicability

uses the Abstract Factory schema

in the following situations
A system should be independent of the creation, composition, and presentation of its product
A system is configured by one of several product families
When you want to emphasize the design of a range of related product objects for joint use
When you provide a product class library, you just want to display their interfaces, not when you implement them
5. structure

to describe the picture here to write

6. participants
AbstractFactory declares an operation interface that creates an abstract product object
ConcreteFactory implements the creation of specific product objects
AbstractProduct declares an interface for a class of product objects
ConcretePrduct defines a product object that will be created by the corresponding concrete factory to implement the AbstractProduct interface
Client uses only interfaces that have declarations of AbstractFacotory and AbstractProduct classes
7. collaboration

usually creates instances of a ConcreteFactory class at run time to create specific product objects. Different objects are created using different factory.

8. effect
It separates specific classes. You just need to control the class that creates the object, and use it to create the product you want, without having to know how it was created
It makes it easy to change the product line. When you change a series of products, you can change it only by changing the specific factory,
It is good for product consistency. It ensures that a range of products is used to coordinate work when using the application
It's hard to support new kinds of products
9. implementation