In our demonstration of the abstract factory design pattern we generally follow the object oriented
approach.
But it's equally possible nowadays to follow a more functional approach so let's take a look at how
you would build a similar kind of factory but without necessarily relying directly on factories as such.
Because instead of these factories what you can do is you can have simple lambda functions describing
how a particular drink is made.
So let me show you how this works so if you have a class called Drink with volume factory so we're going
to provide the volume of the drink that we actually want to make what we can do is we can actually configure
it as follows.
We're also going to have a map but instead of having a map from the name of the drink to the factory
which actually creates the drink what we're going to do is we're going to have a map from the name of
the drink.
So in this case that's going to be a string once again we're going to map to a function and this function
will define how we drink is actually made so instead of using a factory we're going to basically just
use an anonymous function.
So here I'll define a function which returns a unique TTR of hot drink.
And it takes no arguments like so and we're going to call it factories.
Now we need another header for this one and we need the function of hether.
So let's include functional.
There we go.
And now coming back here what we can do is we can initialize this whole thing.
So let's actually let me have a public member here.
All right.
So the idea is that when you have a constructor.
So you have drink with all the factory as a constructor.
What you do is you initialize the factories by just providing the lambda in place.
So for example if you want to specify that when somebody makes tea you need to 100 milliliters.
And when you make coffee need 50 you can define it in Alamdar whereas here as you can see with kind
of we've been forced to hard code the 200.
Now I know you could sort of take it into an argument of make drink but here is going to be a lot easier
to provide the defaults.
So what's going to happen is we're going to save factories.
T equals.
And then here we provide the lambda so you can have some sort of capture list and we're going to have
any arguments really so it's just going to be auto T equals make unique t.
So you describe the process of making something in a functional way.
And here we can call the prepare so we can call the prepare function with let's say 100 milliliters
and then we can return to you like so.
So this is how you would make a more of a functional factory rather than a factory depending upon this
idea of object orientation and inheritance in the abstract factory design pattern.
And when it comes to actually serving the dream you can do something like the following so you would
maybe have a function which has returns a unique VTR of a hot drink.
You might call it drink.
It would take the name of the drink as before.
So kind of similar to this one.
And here you would simply invoke whatever the factory method is.
So essentially when you provide this Lamda when you're specifying this lambda here this is the factory
method.
It's just encapsulated as a standalone function.
And when it comes to making the during So you return.
So you get the factories you find a factory with a particular name and because it's a function you simply
invoke it with VRAM brackets.
That's all that you need to do.
And this way you'll end up with a functional factory as opposed to an abstract factory that's based
on inheritance.