AlgoViz

Abstract factory

medium 50 min

The Abstract factory creates families of objects that must fit together, without the client knowing a single concrete class.

Watch lesson4:44

Where it shows up

The same pattern appears across everyday software.

  • Java AWT Toolkit

    Abstract superclass of all implementations of the Abstract Window Toolkit. Its subclasses bind components to a particular native toolkit, and Toolkit.getDefaultToolkit returns the one for this machine.

  • Swing look and feel

    A LookAndFeel supplies a matching ComponentUI class for each of Swing's user interface ids through UIDefaults. The family is a table of names, a table-driven form of the idea.

  • .NET DbProviderFactory

    Represents a set of methods for creating a provider's implementation of the data source classes: CreateConnection, CreateCommand, CreateParameter and more.

  • java.nio.file.FileSystem

    Documented as the factory for objects to access files and other objects in the file system: paths, path matchers, watch services. A zip file can be another family.

  • JDBC Connection

    A connection creates statements, prepared statements, arrays, blobs and clobs of its driver, all bound to that connection.

  • WebGPU GPUDevice

    A device creates buffers, textures and pipelines. The specification calls an object valid to use with another only if both have the same device: an owner check.

  • Django database backends

    The PostgreSQL DatabaseWrapper names its matching classes as attributes: client_class, creation_class, features_class, introspection_class, ops_class. A family as data.

  • DOM Document

    document.createElement, createTextNode and the other create methods make nodes that belong to this document. A node of another document has to be adopted or imported.

  • Swing HTMLFactory

    A ViewFactory for HTML. It is related but different: one create method that picks a View by tag, so it keeps no family of kinds. A good example of what is not an abstract factory.

  • React Native platform files

    BigButton.ios.js and BigButton.android.js are picked by the bundler for the running platform. The family is chosen at build time, by file name, without factory objects.