Generation pattern ( English creational patterns ) are a subset of the design pattern from the field of software development , which serve to create objects. They decouple the construction of an object from its representation . The object creation is encapsulated and outsourced (e.g. in a factory ) in order to keep the context of the object creation independent of the concrete implementation, according to the rule: "Program on the interface, not on the implementation!"
Design patterns involve two ideas: the first is to hide knowledge about the specific classes, the second is to hide how instances of those classes are created and connected.
Design patterns are divided into object design patterns and class design patterns. The former deal with design patterns that have to do with object creation and leave parts of object creation to other objects. The latter have to do with class instantiation and leave class instantiation to subclasses.
Example of a generation pattern
If instead of
konto = new GiroKonto();
a factory method is used,
KontoFactory kontoFactory = new KontoFactory(); typGiro = kontoFactory.GIRO; konto = kontoFactory.getInstance(typGiro);
you get the flexibility to determine the class of the object to be created (here current account) at runtime instead of having to commit to the specific class at compile time.
The reasons for the decoupling of construction and representation of an object are different depending on the creation pattern:
Brief description and demarcation of various generation patterns
- Abstract Factory ( abstract factory )
- The abstract factory defines an interface for creating a family of objects, with the specific classes of the objects to be created not being defined until runtime.
- Single item ( singleton )
- An object should only be created once from a class, e.g. B. because a central structure is to be enforced or a corresponding hardware component physically only exists once.
- Builders (builder)
- The constructor design pattern separates the construction of complex objects from their concrete representation. This is to enable the same construction process to generate different representations.
- Factory method ( factory method )
- Several related types of objects (classes) implement the same interface, but differ in name and purpose. Now a concrete object of a certain type is to be used in a program context.
- Variant of the singleton pattern that controls the number of objects created, e.g. B. to reduce the number of database connections open at the same time to a value necessary for good overall performance.
- Prototype ( prototype )
- The prototype design pattern uses an object as a template ( prototype ) to create additional objects that can then be modified.
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns . Addison-Wesley, Massachusetts 1995, ISBN 0-201-63361-2 , p. 81.
- Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns . Addison-Wesley, Massachusetts 1995, ISBN 0-201-63361-2 .