Singleton Pattern:

Intent: To create only one instance of a class.

Lets say that we have a logger class that stores all the messages passed to it and prints them back.

Prototype Pattern:

Intent: Specify the kinds of objects to create using prototypical instance, and create new objects by copying this prototype. — Gang of Four

We want to build a team of kayakers and have some fun in the SF bay.

Lets build this team using prototype pattern.

I have a class name Kayaker which has some properties and some methods including clone method which copies the Kayaker object.

Lets see how we can build our team of kayakers:

Factory Pattern:

Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses- As described by Gang of Four

We want to build a company using bots as our employees. We will have a developer bot and a tester bot. Assume that we already have these classes defined somewhere.

Lets develop a bot factory to create the bots.

Cool! Lets start our company then.

Builder Pattern:

Intent: Separating the construction of a complex object from its representation so that same construction process can create different representations.

Our newly started bot company is growing and we need more type of bots such as Manager bot and DevOps bot. Instead of complicating the constructor with bunch of arguments, we will have a bot builder which will create custom bots for us. Lets see how:

Now we can expand and manage our company work efficiently:

Thats it for creational design patterns. If you liked this post please hit the clap button.

Ready for Structural design patterns? Click here .