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


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 Prototype | With Prototype |
Manually set all fields | Clone from base template |
Expensive & repetitive | Fast and efficient cloning |
Risk of typo or mistakes | Less 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 š©āš»
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.