The human life of the Factory Method

Tsvetan Petrov
4 min readJun 14, 2021

Coding can be fun!

No seriously, it can! You don’t believe me? (Please say you don’t).

Thank you…phew, almost lost your attention, right.

OK, up to the point.

First of all, Hi. :) Happy that you are here and reading this article. This means a lot to me. Hopefully we are not relatives and you can leave some feedback other than:

“GREAT JOB, Tsvetan!!! Amazing writer. Wohooo. Top coder!!!”

You get my point…hopefully.

Now…why did the universe dragged you here? Don’t know, but I can tell you something smart. Coding is interesting and you know what? (Please ask “What?”) People invented programming and that means that the development methods, and everything in that realm represents real life people-to-people/animal/bug communications.

So, how do I know that? Why do I actually think about this? Do I have something else to do other than being smart online?

The answer to all of that is YES!

So, I like metaphors and for me one way to remember or EVEN understand something is by transforming it to something else. On that note, you don’t have to be a developer to understand the meaning of what I am going to write about. That is because we as regular people could use design patterns to make our life easier. To understand bureaucracy, how companies work. How people make friends and many more.

What are design patterns?

This is more or less a way of thinking, structuring, organizing and doing things smart. I am not saying the right way. There is now right way in on software development. There is just:

  • Do I need this?
  • What am I going to improve?
  • Do I understand what I would like to do?

And before I dive deeper, please, dear software engineer who is reading this post, this is not a deep technical overview.But If you like to give some feedback to improve the metaphors in what you read, feel free to write them down below.

Factory Method a.k.a. The Candy Machine

Let’s create a factory! But what is inside our factory. This is a building in which we have (in our simplified case), one machine, that creates candy.

Who loves candy?

Ok, this machine generates a ton of candy, from sweet to sour and you guessed it, from sour to sweet. A lot of models.

But how one machine can create so many variations of the final product???

Hmm, and als, we have another thing in our factory. We have 2 shifts, day and night shift. Every shift has it’s own candy gurus.

Before they start the shift, they decide what type of candy they like to do.

To do that, they look into their recipe books and get all the ingredients into one huuuuuuge pod.

After that, they push a button and voila, Candy!

So, where is the design pattern here, Tsvetan?

Let’s start digging down in what I wrote above and apply some terminology to it.

The factory has a machine. The machine can be represented by a single file. Let’s call it “The Candy Factory”.

In that file we can create the big pod. This pod is called an interface.

The interface is used by the candy Guru 1 and Guru 2.They do that by implementing a recipe for candy. That’s why they put all the ingredients into the pod. In our case, the recipe is the class which is implementing the interface.

The push of a button is called a method.

Let’s make this more visual

I like PHP, sorry…

<?php

namespace Tsvetan\FactoryMethod\CandyFactory;

/**
* The Candy Machine
*/
abstract class CandyMachine
{
/**
* The pod in which we add the ingredients
*/
abstract public function recipeCreator(): Candy;

/**
* The magic button
*/
public function makeCandy(): string
{
// Call the recipe creator to create the Candy object.
$candy = $this->recipeCreator();
// Now, use the product.
$result = "We just made " . $candy->cook();

return $result;
}
}

/**
* Our Gurus
*/
class StrawberryCandyRecipe extends CandyMachine
{
/**
*
*/
public function recipeCreator(): Candy
{
return new StrawberryCandy();
}
}

class ChocolateCandyRecipe extends CandyMachine
{
public function recipeCreator(): Candy
{
return new ChocolateCandy();
}
}

/**
* Now, let's create our Candy interface
*/
interface Candy
{
public function cook(): string;
}

/**
* Concrete Products provide various implementations of the Product interface.
*/
class StrawberryCandy implements Candy
{
public function cook(): string
{
return "strawberry candy";
}
}

class ChocolateCandy implements Candy
{
public function cook(): string
{
return "chocolate candy";
}
}

/**
* The Guru magic hands
*/
function abraCadabra(CandyMachine $candyMachine)
{
// ...
echo "This is an example of how a guru can use a candy machine to create "
. $candyMachine->makeCandy();
// ...
}

// Guru 1
abraCadabra
(new StrawberryCandyRecipe());
echo "\n\n";

// Guru 2
abraCadabra
(new ChocolateCandyRecipe());

Before you go

Thank you for reading up to this point.

Hope that I managed to show you something new or interesting.

I am planning of writing more posts like this one in which we can dive deeper into the human life of the Builder Pattern.

--

--