Abstract Factory Model
Abstract Factory Model
Intent: provide an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
Practicality
- A system must be independent of the creation, combination, and representation of its products.
- A system must be configured by one of multiple product generations.
- When you want to emphasize the design of a series of related product objects for joint use.
- When you provide a product class library, instead of displaying their interfaces.
Structure
Abstract Factory has multiple factory methods. The number of factory methods is generally the number of object product types. Products returned from the same factory method are generally used together. The abstract factory mode is usually implemented using the factory method mode, or Prototype. A specific factory is usually Singleton.
Participants
- AbstractFactory: declares an operation interface for creating a series of product objects.
- ConcreteFactory: allows you to create specific product objects.
- AbstractProduct: declares an interface for a type of product object
- ConcreteProduct: Define a product object created by a specific factory.
- Client: Only interfaces declared by the AbstractFactory and AbstractProduct classes are used.
Collaboration
-Create a ConcreteFactory class instance at runtime. This specific factory creates a specific product object. To create different product objects, customers should use different factories.
-AbstractFactory delays the creation of product objects to its ConcreteFactory subclass.
Effect
-It separates specific classes. The Abstract Factory mode helps you control the classes of objects created by an application.
-It makes it easy to switch between product series. A specific factory class only appears once in an application-that is, when it is initialized.
-It facilitates product consistency.
-It is difficult to support new types of products, and to expand abstract factories to produce new types of products
Abstract Facotry mode in JDK
- Recognizeable by creational methods returning the factory itself which in turn can be used to create another abstract/interface type. (javadoc classes have 'factory ')
- Javax. xml. parsers. DocumentBuilderFactory # newInstance ()
- Javax. xml. transform. TransformerFactory # newInstance ()
- Javax. xml. xpath. XPathFactory # newInstance ()
- DatagramSocketImplFactory, PreferencesFactory
- Java. SQL interfaces all get their concrete implementations from JDBC JAR when driver is registered.
- An abstract factory has multiple factory methods, each creating a different product. the products produced by one factory are intended to be used together (your printer and cartridges better be from the same (abstract) factory ). as mentioned in answers abve the families of awt gui components, differing from platform to platform, are an example of this (although its implementation differs from the structure described in Gof ).