You're a new recruit entering a top-secret Virtual Reality military simulator used to train elite Special Forces. As you load in, the AI Drill Sergeant roars: “Soldier! You don’t bring your own weapons here. We equip you based on mission needs.” 🪖
The Weapon Assembly Console — powered by the GunFactory
— takes care of everything.
Just call out your desired weapon, and it equips you instantly with the perfect loadout for the mission.
̸̸̱͂ ̸͆̿͞ ̄̿̄͞
▄︻デ╦═一━
⌐╦╦═─
▄︻╦芫≡══--
︻╦̵̵̿╤━─
Problem:
If each weapon creation had its own logic scattered throughout the system, it would lead to duplicate code, poor scalability, and high maintenance. Adding or modifying weapons would become a mess.
🏭 Factory Pattern Solution:
The GunFactory
encapsulates weapon creation. It hides the construction details and provides
a clean interface to generate any weapon dynamically. You just ask — the factory builds and delivers.
UML Diagram:
Output:
=== Weapon Test Firing === 1️⃣ Firing Pistol: ̸̸̱͂ ̸͆̿͞ ̄̿̄͞ ̿̅͞ ̿̅͞ ̄̚ 2️⃣ Firing Rifle: ▄︻デ╦═一━ 3️⃣ Firing Shotgun: ⌐╦╦═─ 4️⃣ Firing AK47: ▄︻╦芫≡══-- 5️⃣ Firing Sniper Rifle: ︻╦̵̵̿╤━─ === All weapons fired! ===