Html/Javascript widget

Friday 26 August 2022

Factory Method

Factory method is a abstract factory, builder, factory method, prototype and singleton. creational pattern that uses factory methods to creat objects without specifying the exact class of the object being created. This is done by creating objects by calling a factory method- either specified in an interface and implemented by child classes or implemented in a base class and optionally overridden by derived classes- rather than by calling a constructor.

The Factory Method design pattern solves problems like:

  • How can an object be created so that subclasses can redefine which class to instantiate?
  • How can a class defer instantiation to subclasses?

The Factory Method design pattern describes how to solve such problems:

  • Define a separate operation (factory method) for creating an object.
  • Create an object by calling a factory method.

This enables writing of subclasses to change the way an object is created.

Definition

Defines an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.

The factory method pattern relies on inheritance, as object creation is delegated to subclasses that implement the factory method to create objects.

No comments:

Post a Comment