Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Factory Pattern

For the creation of objects on demand

Encapsulates object creation in one place [DRY] without


specifying their concreate classes

Not truly a pattern

Python

def create_factory(cls):
def _create_record(*args, **kwargs):
return cls(*args, **kwargs)
return _create_record

class Food:
...

food_factory = create_factory(Food)

product1 = food_factory()

In simple terms, a garage where product is created.

food_factory supports for the addition of a few more methods


for creation, and the caller need not bother

food_factory = create_factory(NewFood) can be the new


factory, where Food is replace by NewFactory.

Factory Pattern 1

You might also like