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. [1]
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).
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." [2]
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. [3] The pattern can also rely on the implementation of an interface.
In the above UML class diagram, the Creator
class that requires a Product
object does not instantiate the Product1
class directly. Instead, the Creator
refers to a separate factoryMethod()
to create a product object, which makes the Creator
independent of the exact concrete class that is instantiated. Subclasses of Creator
can redefine which class to instantiate. In this example, the Creator1
subclass implements the abstract factoryMethod()
by instantiating the Product1
class.
This C++14 implementation is based on the pre C++98 implementation in the book. [5] [ which? ]
#include<iostream>#include<memory>enumProductId{MINE,YOURS};// defines the interface of objects the factory method creates.classProduct{public:virtualvoidprint()=0;virtual~Product()=default;};// implements the Product interface.classConcreteProductMINE:publicProduct{public:voidprint(){std::cout<<"this="<<this<<" print MINE\n";}};// implements the Product interface.classConcreteProductYOURS:publicProduct{public:voidprint(){std::cout<<"this="<<this<<" print YOURS\n";}};// declares the factory method, which returns an object of type Product.classCreator{public:virtualstd::unique_ptr<Product>create(ProductIdid){if(ProductId::MINE==id)returnstd::make_unique<ConcreteProductMINE>();if(ProductId::YOURS==id)returnstd::make_unique<ConcreteProductYOURS>();// repeat for remaining products...returnnullptr;}virtual~Creator()=default;};intmain(){// The unique_ptr prevent memory leaks.std::unique_ptr<Creator>creator=std::make_unique<Creator>();std::unique_ptr<Product>product=creator->create(ProductId::MINE);product->print();product=creator->create(ProductId::YOURS);product->print();}
The program output is like
this=0x6e5e90printMINEthis=0x6e62c0printYOURS
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.
Room
is the base class for a final product (MagicRoom
or OrdinaryRoom
). MazeGame
declares the abstract factory method to produce such a base product. MagicRoom
and OrdinaryRoom
are subclasses of the base product implementing the final product. MagicMazeGame
and OrdinaryMazeGame
are subclasses of MazeGame
implementing the factory method producing the final products. Factory methods thus decouple callers (MazeGame
) from the implementation of the concrete classes. This makes the new
operator redundant, allows adherence to the open–closed principle and makes the final product more flexible in the event of change.
// Empty vocabulary of actual objectpublicinterfaceIPerson{stringGetName();}publicclassVillager:IPerson{publicstringGetName(){return"Village Person";}}publicclassCityPerson:IPerson{publicstringGetName(){return"City Person";}}publicenumPersonType{Rural,Urban}/// <summary>/// Implementation of Factory - Used to create objects./// </summary>publicclassPersonFactory{publicIPersonGetPerson(PersonTypetype){switch(type){casePersonType.Rural:returnnewVillager();casePersonType.Urban:returnnewCityPerson();default:thrownewNotSupportedException();}}}
The above code depicts the creation of an interface called IPerson
and two implementations called Villager
and CityPerson
. Based on the type passed to the PersonFactory
object, the original concrete object is returned as the interface IPerson
.
A factory method is just an addition to the PersonFactory
class. It creates the object of the class through interfaces but also allows the subclass to decide which class is instantiated.
publicinterfaceIProduct{stringGetName();boolSetPrice(doubleprice);}publicclassPhone:IProduct{privatedouble_price;publicstringGetName(){return"Apple TouchPad";}publicboolSetPrice(doubleprice){_price=price;returntrue;}}/* Almost same as Factory, just an additional exposure to do something with the created method */publicabstractclassProductAbstractFactory{protectedabstractIProductMakeProduct();publicIProductGetObject()// Implementation of Factory Method.{returnthis.MakeProduct();}}publicclassPhoneConcreteFactory:ProductAbstractFactory{protectedoverrideIProductMakeProduct(){IProductproduct=newPhone();// Do something with the object after receiving itproduct.SetPrice(20.30);returnproduct;}}
In this example, MakeProduct
is used in concreteFactory
. As a result, MakeProduct()
may be invoked in order to retrieve it from the IProduct
. Custom logic could run after the object is obtained in the concrete factory method. GetObject
is made abstract in the factory interface.
This Java example is similar to one in the book Design Patterns.
The MazeGame
uses Room
but delegates the responsibility of creating Room
objects to its subclasses that create the concrete classes. The regular game mode could use this template method:
publicabstractclassRoom{abstractvoidconnect(Roomroom);}publicclassMagicRoomextendsRoom{publicvoidconnect(Roomroom){}}publicclassOrdinaryRoomextendsRoom{publicvoidconnect(Roomroom){}}publicabstractclassMazeGame{privatefinalList<Room>rooms=newArrayList<>();publicMazeGame(){Roomroom1=makeRoom();Roomroom2=makeRoom();room1.connect(room2);rooms.add(room1);rooms.add(room2);}abstractprotectedRoommakeRoom();}
The MazeGame
constructor is a template method that adds some common logic. It refers to the makeRoom()
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 makeRoom
method may be overridden:
publicclassMagicMazeGameextendsMazeGame{@OverrideprotectedMagicRoommakeRoom(){returnnewMagicRoom();}}publicclassOrdinaryMazeGameextendsMazeGame{@OverrideprotectedOrdinaryRoommakeRoom(){returnnewOrdinaryRoom();}}MazeGameordinaryGame=newOrdinaryMazeGame();MazeGamemagicGame=newMagicMazeGame();
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 public
and called directly by the client code (in contrast to the previous Java example).
/* Factory and car interfaces */interfaceCarFactory{publicfunctionmakeCar():Car;}interfaceCar{publicfunctiongetType():string;}/* Concrete implementations of the factory and car */classSedanFactoryimplementsCarFactory{publicfunctionmakeCar():Car{returnnewSedan();}}classSedanimplementsCar{publicfunctiongetType():string{return'Sedan';}}/* Client */$factory=newSedanFactory();$car=$factory->makeCar();print$car->getType();
This Python example employs the same as did the previous Java example.
fromabcimportABC,abstractmethodclassMazeGame(ABC):def__init__(self)->None:self.rooms=[]self._prepare_rooms()def_prepare_rooms(self)->None:room1=self.make_room()room2=self.make_room()room1.connect(room2)self.rooms.append(room1)self.rooms.append(room2)defplay(self)->None:print(f"Playing using {self.rooms[0]}")@abstractmethoddefmake_room(self):raiseNotImplementedError("You should implement this!")classMagicMazeGame(MazeGame):defmake_room(self)->"MagicRoom":returnMagicRoom()classOrdinaryMazeGame(MazeGame):defmake_room(self)->"OrdinaryRoom":returnOrdinaryRoom()classRoom(ABC):def__init__(self)->None:self.connected_rooms=[]defconnect(self,room:"Room")->None:self.connected_rooms.append(room)classMagicRoom(Room):def__str__(self)->str:return"Magic room"classOrdinaryRoom(Room):def__str__(self)->str:return"Ordinary room"ordinaryGame=OrdinaryMazeGame()ordinaryGame.play()magicGame=MagicMazeGame()magicGame.play()
In object-oriented programming, a class defines the shared aspects of objects created from the class. The capabilities of a class differ between programming languages, but generally the shared aspects consist of state (variables) and behavior (methods) that are each either associated with a particular object or with all objects of that class.
A visitor pattern is a software design pattern that separates the algorithm from the object structure. Because of this separation, new operations can be added to existing object structures without modifying the structures. It is one way to follow the open/closed principle in object-oriented programming and software engineering.
Design Patterns: Elements of Reusable Object-Oriented Software (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword by Grady Booch. The book is divided into two parts, with the first two chapters exploring the capabilities and pitfalls of object-oriented programming, and the remaining chapters describing 23 classic software design patterns. The book includes examples in C++ and Smalltalk.
The abstract factory pattern in software engineering is a design pattern that provides a way to create families of related objects without imposing their concrete classes, by encapsulating a group of individual factories that have a common theme without specifying their concrete classes. According to this pattern, a client software component creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the family. The client does not know which concrete objects it receives from each of these internal factories, as it uses only the generic interfaces of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.
The bridge pattern is a design pattern used in software engineering that is meant to "decouple an abstraction from its implementation so that the two can vary independently", introduced by the Gang of Four. The bridge uses encapsulation, aggregation, and can use inheritance to separate responsibilities into different classes.
In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.
In computer programming, the flyweight software design pattern refers to an object that minimizes memory usage by sharing some of its data with other similar objects. The flyweight pattern is one of twenty-three well-known GoF design patterns. These patterns promote flexible object-oriented software design, which is easier to implement, change, test, and reuse.
The prototype pattern is a creational design pattern in software development. It is used when the types of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to avoid subclasses of an object creator in the client application, like the factory method pattern does, and to avoid the inherent cost of creating a new object in the standard way when it is prohibitively expensive for a given application.
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, dynamically, without affecting the behavior of other instances of the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern as well as to the Open-Closed Principle, by allowing the functionality of a class to be extended without being modified. Decorator use can be more efficient than subclassing, because an object's behavior can be augmented without defining an entirely new object.
In software design and engineering, the observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.
In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method.
In object-oriented programming such as is often used in C++ and Object Pascal, a virtual function or virtual method is an inheritable and overridable function or method that is dispatched dynamically. Virtual functions are an important part of (runtime) polymorphism in object-oriented programming (OOP). They allow for the execution of target functions that were not precisely identified at compile time.
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or in added complexity to the design due to inflexibility in the creation procedures. Creational design patterns solve this problem by somehow controlling this object creation.
In object-oriented programming, a factory is an object for creating other objects; formally, it is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be new. More broadly, a subroutine that returns a new object may be referred to as a factory, as in factory method or factory function. The factory pattern is the basis for a number of related software design patterns.
Modern C++ Design: Generic Programming and Design Patterns Applied is a book written by Andrei Alexandrescu, published in 2001 by Addison-Wesley. It has been regarded as "one of the most important C++ books" by Scott Meyers.
In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes. In most class-based object-oriented languages like C++, an object created through inheritance, a "child object", acquires all the properties and behaviors of the "parent object", with the exception of: constructors, destructors, overloaded operators and friend functions of the base class. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors, to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed acyclic graph.
In programming languages, an abstract type is a type in a nominative type system that cannot be instantiated directly; by contrast, a concrete typecan be instantiated directly. Instantiation of an abstract type can occur only indirectly, via a concrete subtype.
The curiously recurring template pattern (CRTP) is an idiom, originally in C++, in which a class X
derives from a class template instantiation using X
itself as a template argument. More generally it is known as F-bound polymorphism, and it is a form of F-bounded quantification.
In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL). The term was coined in 2005 by Eric Evans and Martin Fowler.
In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior, was first published as "Void Value" and later in the Pattern Languages of Program Design book series as "Null Object".