šŸ“…Week-8 (Day-1) - How to Implement Prototype Design Pattern: A Guide with UML and Code

Payal KumariPayal Kumari
5 min read

NOTE: - I started my 8-week system design journey with Coder Army. I will be journaling every day, recording what I learn, reflecting on it, and sharing it with my network to help newcomers to system design.

Namaste developers! šŸ‘‹
Welcome to another power-packed day of the #8WeeksLLDChallenge — and today, we’re diving into something that every large system needs but is often overlooked — the Mediator Design Pattern!

Aaj ka challenge: Chat Room System — but with a twist!
Instead of users communicating with each other directly (which creates chaos in large systems), we’ll use a Mediator to handle message passing — creating a cleaner, more scalable architecture.

šŸ’  What is the Prototype Design Pattern?

The Prototype Pattern allows you to create new objects by cloning existing ones, instead of building them from scratch every time.

(Hindi: Prototype Pattern aapko naye object banane ke liye existing object ka clone banane ki suvidha deta hai. Agar ek baar object bana liya hai, toh baar-baar naye create karne ki zarurat nahi — usi ka duplicate use karke kaam ho jata hai.)

šŸ’ Why Use It?

Creating a complex object from scratch every time can be:

  • āŒ Time-consuming

  • āŒ Expensive (resource-heavy calculations, DB calls, setup)

  • āŒ Error-prone (repeat same values again & again)

Instead, just clone an existing template object and tweak it!

šŸ’ Real-Life Analogy

NPC Cloning in Games!

Imagine you're building a game like PUBG or Free Fire šŸŽ®, and you need to create 100 aliens (Non-Player Characters).

  • Without prototype: har alien ke liye manually stats bharne padenge

  • With prototype: ek baar ek alien banao, baaki sab copy-paste (clone)!

(Hindi: Ek hi base alien se 100 copy banao — time bacha, kaam asaan hua )

šŸ’ UML Diagram

šŸ’ Code

šŸ“without Prototype Pattern

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Simple NPC class — no Prototype
class NPC {
public:
    string name;
    int health;
    int attack;
    int defense;

    // ā€œHeavyā€ constructor: every field must be provided
    NPC(const string& name, int health, int attack, int defense) {

        // call database
        // complex calc
        this->name = name;
        this->health = health;
        this->attack = attack;
        this->defense = defense;

        cout << "Creating NPC '" << name << "' [HP:" << health << ", ATK:" 
             << attack << ", DEF:" << defense << "]\n";
    }

    void describe() {
        cout << "  NPC: " << name << " | HP=" << health << " ATK=" << attack
             << " DEF=" << defense << "\n";
    }
};

int main() {
    // Base Alien
    NPC* alien = new NPC("Alien", 30, 5, 2);
    alien->describe();

    // Powerful Alien — must re-pass all stats, easy to make mistakes
    NPC* alien2 = new NPC("Powerful Alien", 30, 5, 5);
    alien2->describe();

    // If you want 100 aliens, you'd repeat this 100 times…

    // cleanup
    delete alien;
    delete alien2;
    return 0;
}

Benefits:

Every time you want a new NPC, you have to:

  • Call constructor

  • Refill all values again

  • Risk mistakes and redundancy

šŸ“with Prototype Pattern

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Cloneable (aka Prototype) interface
class Cloneable {
public:
    virtual Cloneable* clone() const = 0;
    virtual ~Cloneable() {}
};

class NPC : public Cloneable {
public:
    string name;
    int health;
    int attack;
    int defense;

    NPC(const string& name, int health, int attack, int defense) {
        // call database
        // complex calc
        this->name = name; 
        this->health = health; 
        this->attack = attack; 
        this->defense = defense;
        cout << "Setting up template NPC '" << name << "'\n";
    }

    // copy‐ctor used by clone()
    NPC(const NPC& other) {
        name = other.name;
        health = other.health;
        attack = other.attack;
        defense = other.defense;
        cout << "Cloning NPC '" << name << "'\n";
    }

    // the clone method required by Prototype
    Cloneable* clone() const override {
        return new NPC(*this);
    }

    void describe() {
        cout << "NPC " << name  << " [HP=" << health  << " ATK=" << attack 
             << " DEF=" << defense << "]\n";
    }

    // setters to tweak the clone…
    void setName(const string& n) { 
        name = n;
    }
    void setHealth(int h) { 
        health = h;
    }
    void setAttack(int a) {
         attack = a; 
    }
    void setDefense(int d){ 
        defense = d;
    }
};

int main() {
    // 1) build one ā€œheavyā€ template
    NPC* alien = new NPC("Alien", 30, 5, 2);

    // 2) quickly clone + tweak as many variants as you like:
    NPC* alienCopied1 = dynamic_cast<NPC*>(alien->clone());
    alienCopied1->describe();

    NPC* alienCopied2 = dynamic_cast<NPC*>(alien->clone());
    alienCopied2->setName("Powerful Alien");
    alienCopied2->setHealth(50);
    alienCopied2->describe();

    // cleanup
    delete alien;
    delete alienCopied1;
    delete alienCopied2;
}

Benefits:

  • Only one object is setup-heavy

  • Rest are just copied (cloned)

  • Easy to customize each new object

  • Super efficient for games, simulations, testing, prototyping

šŸ’ Advantages of Prototype Pattern

āœ… Performance boost (no re-computation)
āœ… Reduced memory/resource consumption
āœ… Makes code DRY (Don’t Repeat Yourself)
āœ… Super useful in games, simulations, design tools, IDEs

šŸ’ When to Use

Use Prototype Pattern when:

  • Object creation is expensive or slow

  • You need many similar instances with small changes

  • You're building test data, simulations, or design templates

šŸ’  Summary

Without PrototypeWith Prototype
Manually set all fieldsClone from base template
Expensive & repetitiveFast and efficient cloning
Risk of typo or mistakesLess chance of error

šŸ’ Final Thoughts

Prototype Pattern isn’t just a design pattern — it’s a developer’s shortcut when you need speed, scale, and simplicity

(Hindi: Prototype pattern aapki development ko fast aur efficient banata hai — kam coding, zyada cloning)

Week - 8 (Day-1) Completed āœ… System Design

NOTE : - A big thanks to my mentors Rohit Negi Sir and Aditya Sir for launching this amazing 8-week course absolutely free on YouTube via CoderArmy9 :- youtube.com/@CoderArmy9 . šŸ™Œ

šŸ‘‰ Share this blog with your connections! Let’s keep learning, growing, and supporting one another on this journey. šŸš€

āœļø Payal Kumari šŸ‘©ā€šŸ’»

Jai Hind šŸ‡®šŸ‡³ | #CoderArmy #LearningInPublic #SystemDesign #TechForAll #MentorshipMatters #8weeksLLdChallenge #LowLevelDesign #LLD šŸ‘©ā€šŸ’»

0
Subscribe to my newsletter

Read articles from Payal Kumari directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Kumari
Payal Kumari

I'm a passionate full-stack developer with a strong foundation in the MERN stack—building and maintaining scalable web applications using React.js, Node.js, and Next.js. My journey in open source began with Hacktoberfest 2023, where I made four impactful pull requests that sparked a love for collaborative coding, global learning, and open knowledge sharing. Since then, I’ve contributed to and mentored projects in top open source programs like GSSoC’24, SSOC’24, and C4GT’24. As a Google Gen AI Exchange Hackathon ’24 Finalist and Google’s Women Techmakers (WTM) Ambassador, I’ve been privileged to support diverse communities in building meaningful tech solutions. My work as a Top 50 Mentor for GSSoC ’24 reflects my commitment to nurturing new talent in tech. Beyond development, I serve as a Student Career Guide, Profile Building Expert & Evangelist at Topmate.io, where I conduct workshops, guide students through resume building and career strategy, and help mentees navigate open source and tech careers. Recognized among the Top 5% of mentors and featured on ā€œTopmate Discover,ā€ I take pride in making mentorship accessible and impactful. My technical voice has also been acknowledged by LinkedIn, where I’ve earned the Top Voice badge seven times in domains like web development, programming, and software engineering. In addition, I hold LinkedIn Golden Badges for Research Skills, Interpersonal Skills, Critical Thinking, and Teamwork—signaling a well-rounded approach to both individual contribution and team collaboration. Graduating with an MCA from Chandigarh University in 2023, I’ve continued to fuel my curiosity by writing technical articles and sharing practical MERN stack insights across platforms. Whether it’s building polished UIs, optimizing backend performance, or guiding a mentee through their first pull request, I’m driven by the power of community and continuous learning. Let’s connect! I'm open to collaborations, mentorship, or building something impactful together. Reach out to me at kumaripayal7488@gmail.com or visit my profile on Topmate.io.