Factory method pattern

1

In object-oriented programming, the factory method pattern is a design pattern that uses factory methods to deal with the problem of creating objects without having to specify their exact classes. Rather than by calling a constructor, this is accomplished by invoking a factory method to create an object. Factory methods can be specified in an interface and implemented by subclasses or implemented in a base class and optionally overridden by subclasses. It is one of the 23 classic design patterns described in the book Design Patterns (often referred to as the "Gang of Four" or simply "GoF") and is subcategorized as a creational pattern.

Overview

The factory method design pattern solves problems such as: This enables the creation of subclasses that can change the way in which an object is created (for example, by redefining which class to instantiate).

Definition

According to Design Patterns: Elements of Reusable Object-Oriented Software: "Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclasses." Creating an object often requires complex processes not appropriate to include within a composing object. The object's creation may lead to a significant duplication of code, may require information inaccessible to the composing object, may not provide a sufficient level of abstraction or may otherwise not be included in the composing object's concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created. The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects. The pattern can also rely on the implementation of an interface.

Structure

UML class diagram

In the above UML class diagram, the class that requires a object does not instantiate the class directly. Instead, the refers to a separate to create a product object, which makes the independent of the exact concrete class that is instantiated. Subclasses of can redefine which class to instantiate. In this example, the subclass implements the abstract by instantiating the class.

Examples

This C++14 implementation is based on the pre C++98 implementation in the book. The program output is like A maze game may be played in two modes, one with regular rooms that are only connected with adjacent rooms, and one with magic rooms that allow players to be transported at random.

Structure

is the base class for a final product ( or ). declares the abstract factory method to produce such a base product. and are subclasses of the base product implementing the final product. and are subclasses of implementing the factory method producing the final products. Factory methods thus decouple callers from the implementation of the concrete classes. This makes the operator redundant, allows adherence to the open–closed principle and makes the final product more flexible in the event of change.

Example implementations

C#

The above code depicts the creation of an interface called and two implementations called and. Based on the type passed to the object, the original concrete object is returned as the interface. A factory method is just an addition to the class. It creates the object of the class through interfaces but also allows the subclass to decide which class is instantiated. In this example, is used in. As a result, may be invoked in order to retrieve it from the. Custom logic could run after the object is obtained in the concrete factory method. is made abstract in the factory interface.

Java

This Java example is similar to one in the book Design Patterns. The uses but delegates the responsibility of creating objects to its subclasses that create the concrete classes. The regular game mode could use this template method: The constructor is a template method that adds some common logic. It refers to the factory method that encapsulates the creation of rooms such that other rooms can be used in a subclass. To implement the other game mode that has magic rooms, the method may be overridden:

PHP

This PHP example shows interface implementations instead of subclassing (however, the same can be achieved through subclassing). The factory method can also be defined as and called directly by the client code (in contrast to the previous Java example).

Python

This Python example employs the same as did the previous Java example.

Uses

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

View original