Prototype Examples:

Java
Java
C#
C#
PHP
PHP
Python
Python
TypeScript
TypeScript
▸ Prototype Quick Review

Making a PC Builder in C#

Building a computer is one of the many things that has been made easier than ever thanks to online part ordering websites. One important design consideration when making a PC building website is how to implement a starter kit feature where customers can start with a recommended set of parts, and swap out only specific ones as they see fit. This is great for users who aren't as tech savvy, but still have a general idea of what they'd like their new machine to do.

If we were to implement our own starter kit feature, we could make use of the Prototype Pattern to create a catalogue of base model computers that our customers can choose from.

Computer Implementation:

public interface IComputer
{
   // Clone Method:
   IComputer Clone();
}


// The Desktop class for a computer type:
public class Desktop: IComputer
{
   public string cpu;
   public string gpu;
   public int gigsOfRam;

   public Desktop()
   {
       this.cpu = "C1";
       this.gpu = "Integrated Graphics";
       this.gigsOfRam = 2;
   }

   // Copy constructor: helps with the clone method!
   public Desktop(Desktop prototype)
   {
       this.cpu = prototype.cpu;
       this.gpu = prototype.gpu;
       this.gigsOfRam = prototype.gigsOfRam;
   }

   // Clone implementation:
   public IComputer Clone()
   {
       return new Desktop(this);
   }
}

Computer Catalogue

public class DesktopCatalogue
{
   // A Map of the cataloged items:
   Dictionary<string, IComputer> catalogue;

   public DesktopCatalogue()
   {
      this.catalogue = new Dictionary<string, IComputer>();
   }

   public void Add(string key, IComputer prototype)
   {
       // Add in a *clone* of the prototype to prevent editing its properties!
       catalogue.Add(key, prototype.Clone());
   }

   public IComputer GetComputer(string key)
   {
       if(catalogue.ContainsKey(key))
       {
           // Return a *clone* of the catalogued prototype!
           return catalogue[key].Clone();
       }
       else
       {
           throw new System.Exception("No Computer of this key exists.");
       }
   }
}

Demo of the Prototype Pattern:

public class Solution
{
   public static void Main(string[] args)
   {
       // Create the catalogue:
       DesktopCatalogue catalogue = new DesktopCatalogue();

       // Create the first prototype:
       Desktop basicWorkstation = new Desktop();
       basicWorkstation.cpu  = "j5";
       basicWorkstation.gpu = "cornea Graphics 9001";
       basicWorkstation.gigsOfRam = 4;

       // Throw it in the catalogue:
       catalogue.Add("Everyday Computing", basicWorkstation);

       // Create another prototype:
       Desktop bigGamerSetup = new Desktop();
       bigGamerSetup.cpu = "X1 Hyper Thread";
       bigGamerSetup.gpu = "Hive 7series";
       bigGamerSetup.gigsOfRam = 1024;

       // Add into the mix:
       catalogue.Add("Gaming", bigGamerSetup);

       // Using the Prototypes:
       
       // Get a computer from the Catalogue:
       Desktop myBasicComputer = (Desktop)catalogue.GetComputer("Everyday Computing");
       // Let's add a bit more ram as an upgraded version:
       myBasicComputer.gigsOfRam = 8;


       // Get another one:
       Desktop mySuperComputer = (Desktop)catalogue.GetComputer("Gaming");
       // Add even more ram!
       mySuperComputer.gigsOfRam = 4096;
       mySuperComputer.cpu = "Octa-Streamed X1 Giga Thread";
       mySuperComputer.gpu = "Quad Hive 9series";
       
   }
}

Find any bugs in the code? let us know!