Factory Method Pattern

The factory method pattern is an object-oriented creational design pattern to implement the concept of factories and deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The essence of this pattern is to "Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The 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 not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of 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.

Some of the processes required in the creation of an object include determining which object to create, managing the lifetime of the object, and managing specialized build-up and tear-down concerns of the object. Outside the scope of design patterns, the term factory method can also refer to a method of a factory whose main purpose is creation of objects.

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

Read more about Factory Method Pattern:  Applicability, Other Benefits and Variants, Limitations, Uses

Famous quotes containing the words factory, method and/or pattern:

    I reverently believe that the Maker who made us all makes everything in New England but the weather. I don’t know who makes that, but I think it must be raw apprentices in the weather-clerk’s factory who experiment and learn how.... In the spring I have counted one hundred and thirty-six different kinds of weather inside of four-and-twenty hours.
    Mark Twain [Samuel Langhorne Clemens] (1835–1910)

    Women are denied masturbation even more severely than men and that’s another method of control—they’re not taught to please themselves.... Most women—it takes them a while to warm up to the “situation” but once they get into it, I’m sure they’re going to get just as hooked as—well, everyone I know is!
    Lydia Lunch (b. 1959)

    For the man who should loose me is dead,
    Fighting with the Duke in Flanders,
    In a pattern called a war.
    Christ! What are patterns for?
    Amy Lowell (1874–1925)