Abstract factory

from Wikipedia, the free encyclopedia

The abstract factory ( English abstract factory , kit ) is a design pattern in the field of software development , which the category of generation pattern (English creational patterns ) belongs. It defines an interface for creating a family of objects, whereby the specific classes of the objects to be instantiated are not specified in more detail. The pattern is one of the design patterns published by the so-called Gang of Four (GoF).

use

The abstract factory is applied when

  • a system should work regardless of how its products are produced,
  • a system is to be configured with one or more product families,
  • a group of products is to be created and shared, or
  • if the interfaces of products are to be provided in a class library without their implementation.

A typical application is the creation of a graphical user interface with different surface motifs .

An abstract factory combines the responsibilities of “summarizing the generation of objects in one place” and “possibility of abstract constructors” (see also below under “Related design patterns”).

UML diagram: Abstract Factory

actors

AbstrakteFabrik
defines an interface for generating abstract products of a product family
KonkreteFabrik
creates concrete products of a product family by implementing the interface
AbstraktesProdukt
defines an interface for a product type
KonkretesProdukt
defines a specific product of a product type by implementing the interface, is generated by the corresponding specific factory
Klient
uses the interfaces of the abstract factory and abstract products

advantages

  • The client is isolated from concrete classes.
  • The exchange of product families is possible in a simple manner.

disadvantage

It is difficult to add new types of products because changes need to be made in all specific factories.

Use in analysis

Because of the common complexity of the two main responsibilities (“summary of object generation in one place” and “possibility of abstract constructors”) this design pattern is practically irrelevant for the analysis.

example

A game collection is to be developed using software. The classes used are included

  1. Game board (first abstract product) on which playing pieces can be placed and which, for example, has a method of displaying itself on the screen. Concrete, derived products are chess , mill board , Halma board etc.
  2. Game figure (second abstract product) that can be placed on a game board. Concrete products derived therefrom are hats , chess pieces (for the sake of simplicity there should only be one type of chess pieces here), wooden stones, etc.
  3. Spielfabrik (abstract factory) that creates the components (board, pawns ) of a parlor game. Specific factories derived therefrom are, for example, mill factory , queen factory , chess factory , halma factory etc.

A client (e.g. an instance of a player or game master class) can use the abstract factory to create game pieces or a game board. Depending on which specific game is being played, for example

  • the chess factory a chess board and chess pieces,
  • the checkers' factory also has a chessboard, but wooden stones,
  • the mill factory a mill board, but also wooden stones,
  • the Halmafabrik a straw board and hat.

Programming example in PHP

// abstraktes Produkt A
abstract class Spielbrett {
    abstract function aufstellen();
}
// abstraktes Produkt B
abstract class Spielfigur {
    abstract function bewegen();
}
// abstrakte Fabrik
abstract class Spielfabrik {
    abstract function SpielbrettErzeugen();
    abstract function SpielfigurErzeugen();
}
// konkrete Fabrik 1
class Spielfabrik1 extends Spielfabrik {
    public function SpielbrettErzeugen() {
        return new Spielbrett1();
    }
    public function SpielfigurErzeugen() {
        return new Spielfigur1();
    }
}
// konkrete Fabrik 2
class Spielfabrik2 extends Spielfabrik {
    public function SpielbrettErzeugen() {
        return new Spielbrett2();
    }
    public function SpielfigurErzeugen() {
        return new Spielfigur2();
    }
}

class Spielbrett1 extends Spielbrett {
    public function aufstellen() {
        write_line("Spielbrett1 aufgestellt.");
    }
}
class Spielfigur1 extends Spielfigur {
    public function bewegen() {
        write_line("Spielfigur1 bewegt.");
    }
}

class Spielbrett2 extends Spielbrett {
    public function aufstellen() {
        write_line("Spielbrett2 aufgestellt.");
    }
}
class Spielfigur2 extends Spielfigur {
    public function bewegen() {
        write_line("Spielfigur2 bewegt.");
    }
}

function Test(Spielfabrik $fabrik) {
    $brett = $fabrik->SpielbrettErzeugen();
    $figur = $fabrik->SpielfigurErzeugen();
    $brett->aufstellen();
    $figur->bewegen();
}

function write_line($text) {
    print $text.'<br/>';
}

Test(new Spielfabrik1());
Test(new Spielfabrik2());

Output:

Spielbrett1 aufgestellt.
Spielfigur1 bewegt.
Spielbrett2 aufgestellt.
Spielfigur2 bewegt.

Related design patterns

The abstract factory is simply a multiple application of the factory method . The abstract factory can therefore make an entire product family interchangeable, while the factory method only relates to one product.

If an additional hierarchy of factories to a hierarchy of products is generally to be avoided, the prototype can be used. In this pattern, prototypical instances are copied to create new objects.

Web links

Commons : Abstract Factory  - Album containing pictures, videos and audio files

Individual evidence

  1. Erich Gamma , Richard Helm , Ralph Johnson , John Vlissides : Design pattern . 5th edition. Addison-Wesley , 1996, ISBN 3-8273-1862-9 , pp. 107 .
  2. ^ Karl Eilebrecht, Gernot Starke: Patterns compact. Design patterns for effective software development . 3. Edition. Spektrum Akademischer Verlag, 2010, ISBN 978-3-8274-2525-6 , pp. 26 , doi : 10.1007 / 978-3-8274-2526-3 .