Refactoring Guru already has the best blog to learn Design patterns. Here I am just trying to give a brief about each design.
Design patterns are solutions to recurring problems and design paradigms.
Creational design patterns provide various mechanisms for object creation, increasing flexibility and the reuse of existing code.
We have around 5 patterns. You can get the Java code here, hope it works, built a couple of years back
Let's dive into each one.......
Factory Method

Factory Method is a design pattern that provides an interface for creating objects in a superclass while allowing subclasses to modify the types of objects that are created.
Imagine a pizza shop where you can order different types of pizza, but the creation process (baking, adding toppings) is handled by specific pizza factories(which I am not sure) for each type.
Another example that makes sense is different types of factories for other kinds of cars.
Let's see a class diagram for the Factory class.
Quick Explanation of the Diagram
- Product (Interface): Defines the interface of objects that the factory method creates.
- ConcreteProduct: The actual implementation of the Product.
-
Creator (Abstract Class): Declares the factory method (
createProduct). It can also contain core business logic that relies on the Product objects. - ConcreteCreator: Overrides the factory method to return an instance of a ConcreteProduct.
Abstract Factory Method

Abstract Factory is a design pattern that enables the creation of families of related objects without specifying their concrete classes.
Abstract Factory is a factory of factories, where you build a factory class from Abstract Factory, which in turn builds the type of class you need.

Let's check out the Abstract Factory class diagram. You can clearly see the difference between the factory class and why the abstract factory pattern is Factory of Factories.
Quick Explanation of the Diagram
-
Abstract Factory (Interface): Declares a set of methods for creating each of the abstract products (e.g.,
createProductA,createProductB). -
Concrete Factory: Implements the creation methods to produce a specific flavor or family of products (e.g.,
ConcreteFactory1only createsProductA1andProductB1). -
Abstract Product (Interface): Defines the interface for a type of product (e.g.,
ProductA), ensuring all variations of that product are compatible. - Concrete Product: The specific implementation of a product type, created by the corresponding concrete factory to ensure they match other products in the same family.
Singleton

Singleton is a design pattern that guarantees a class has only one instance while providing a global access point to this instance.
Imagine a single barista/coffee machine, the single instance, that makes all kinds of coffee.
A Singleton class will have a single instance.
Quick Explanation of the Diagram
- Singleton Class: The single class responsible for creating and managing its own unique instance.
-
instance (Private Static Variable): A static member that holds the only instance of the class. The
$symbol (or the underline in standard UML) denotes a static member. -
Singleton() (Private Constructor): A constructor with private visibility that prevents other objects from using the
newoperator with this class. -
getInstance() (Public Static Method): The global access point. When called, it checks if the
instanceis null; if so, it creates one. It then returns that same instance to every caller.
Prototype

Prototype is a creational design pattern that allows you to copy existing objects without making your code dependent on their classes.
Consider a cloning Machine, which is a good example of a prototype.

Let's check the UML Diagram.

Quick Explanation of the Diagram
-
Prototype (Interface): Declares the interface for cloning itself, usually consisting of a single
clonemethod. - Concrete Prototype: Implements the cloning operation by copying its own data into a new object instance.
- Client: Creates new objects by asking an existing prototype instance to clone itself instead of instantiating a class from scratch.
Builder

The Builder design pattern allows you to construct complex objects step by step. This pattern enables the production of different types and representations of an object using the same construction code.
You travelled to 2050. Now you get a burger-building machine kiosk, which builds it step by step.
Quick Explanation of the Diagram
- Director: Defines the order in which to execute the building steps. It works with a builder object to create a specific product configuration (e.g., a "Vegan Burger" vs. a "Double Cheeseburger").
- Builder (Interface): Declares all possible steps required to build a product. These steps are common to all types of builders.
- Concrete Builder: Provides a specific implementation of the building steps. It is responsible for creating and assembling the parts of the complex object and returning the final product.
- Product: The resulting complex object. Unlike other creational patterns, different builders can produce products that do not follow a common interface (e.g., a "House" product vs. a "Manual" product).
Creational Design Patterns Overview
| Design Pattern | Description | Other Example |
|---|---|---|
| Factory Method | Subclasses decide object type |
PizzaStore with regional pizza creators (NYPizzaStore, ChicagoPizzaStore) |
| Abstract Factory | Creates related object families |
GUIFactory for Windows/Mac button + checkbox pairs |
| Singleton | One instance, global access | Single Logger or DatabaseConnectionPool across app |
| Prototype | Clone objects without class dependency | Duplicate Circle/Rectangle shapes via clone() method |
| Builder | Step-by-step complex object construction |
HouseBuilder for WoodenHouse or StoneHouse variants |








Top comments (0)