Design Pattern: Factory Method Pattern

The Factory Method is a creational design pattern, in which an interface handles the creation of objects without specifying the class of an object that will be created. The subclasses will declare the type of object to be created.

UML Diagram Example

The Product interface is implemented by ProductA and ProductB.

The Creator class holds a createProduct() factory method that returns new product objects. The return type of the factory method must match the target interface, in this case is product.

The Concrete Creators are sub classes of Creator and override the factory method, in order to return different type of product.

The factory method doesn’t have to create new instances all the time. It can be use for returning existing objects.

Leave a comment

Design a site like this with WordPress.com
Get started