Simple Factory

noun

say it: “SIM-pul FAK-tuh-ree” /ˈsɪmpəl ˈfæktəri/

One function or class that is the only place a family of objects is made, picking the class from a name.

What is a simple factory?

A simple factory is one function (or class) that turns a name into the right object, so it's the only place in the app that says new for that family. A bakery's order screen and price board both ask it for "pie", and a new product is one new class and one new line in the factory.

def make_product(kind):
    if kind == "cake":
        return Cake()
    if kind == "pie":
        return Pie()
    raise ValueError("No such product: " + kind)

In a sentence

  • “Pies were on every screen the day they launched, because the simple factory was the only place that made products.”

Level: Journeyman · More OOP terms at this level

Updated