Factories in FP

Question from Muhammad Hamza Chippa

How would you replace Factory design patterns in functional programming?

Before someone else says it - with a function

interface ThingFactory {
    Thing makeThing();
}

...

void work(ThingFactory factory) {
    var thing = factory.makeThing();
    thing.whatever();
}
(defn work [factory]
  (let [thing (factory)]
    (.whatever thing)))

You can also use the exact pattern as is (where the producer method gets a special name) with traits/typeclasses/protocols depending on your FP language


<- Index