overviewA factory method is a method in a subclass that creates an object. Factory Method pattern defines a Creator interface with Factory Methods (“create” methods) that are implemented by sub-classes. According to Design Patterns by GoF, the intent of Factory Method is to, Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method (pattern) lets a class defer instantiation to subclasses. source: GoF In Factory Method pattern, the class that is responsible for creating concrete product objects is said to be a Creator. In our example below, MazeGame, BombedMazeGame, and EnchantedMazeGame are Creator classes. In order to create different types of concrete Products (Wall, Room, Door), different type of concrete Creator classes with their own version of factory methods to create their own version of Wall, Room, or Door will be used. All concrete Creators derived from a base Creator interface or class (e.g. MazeGame) with default way of how to create a Wall, Room, or Door. Only if the default is not suited, the subclass can override the instantiation logic. Factory Method pattern is often built upon by Abstract Factory design pattern. Both differs in their intent. In Factory pattern, a client class delegates the responsibility of object instantiation to another object (a Factory object) via composition whereas the Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation. exampleCreator
[1] A factory method (createMaze()) that creates the final end product resides in the Creator class of the Factory Method pattern. [2-5] Factory methods to be overridden/redefined by subclasses to decide how to instantiate products. comparisonFactory Method vs. Factory
Factory Method vs. Abstract Factory
references
0 Comments
Leave a Reply. |
Categories
All
Archives
May 2020
|