# Prototype Design Pattern ###### tags: `Design Pattern` `Code Design` ## What it is for? By using this pattern, you just need an instance of a class to clone it. Let's think first how would you clone the same thing without using this pattern. I would probably use the code below at first: ``` java== public static void main(String[] args) { OriginalProduct originalProduct = new OriginalProduct("Bag", 100, "Light Blue"); System.out.print(originalProduct.toString()); OriginalProduct clonedProduct = new OriginalProduct("Bag", 100, "Light Blue"); System.out.print(clonedProduct.toString()); } ``` >Disadvantages would be that you would have to know what properties you set before to the thing you want to clone. That would be pretty difficult! Here is when **Prototype Design Pattern** becomes so useful! [color= Red] > ## Structure ### Simple Prototype Design Pattern 1. **An Interface** This will have method of clone to be overrided by those that implement it. ``` java== public interface CloneInterface { public Object clone(); } ``` 2. **A Class** This class needs to be cloned in future. ``` java== public class OriginalProduct implements CloneInterface{ private String name; private int price; private String color; public OriginalProduct(String name, int price, String color){ this.name = name; this.color = color; this.price = price; } @Override public OriginalProduct clone(){ return new OriginalProduct(this.name, this.price, this.color); } @Override public String toString(){ return "This is a " + this.color + " " + this.name + " with price of " + this.price; } } ``` **Simple Prototype Design Pattern Usage:** ``` java== public static void main(String[] args) { OriginalProduct originalProduct = new OriginalProduct("Bag", 100, "Light Blue"); OriginalProduct clonedProduct = originalProduct.clone(); System.out.print(clonedProduct.toString()); } ``` :::info Result: This is a Light Blue Bag with price of 100 ::: ### Deep Prototype Design Pattern You will have 2 classes, that will be implementing the CloneInterface. ++Why is it **Deep** Prototype Design Pattern?++ In this example we have: **1. Clone Interface** ``` java== public interface CloneInterface { public Object clone(); } ``` **2. Character Class implementing CloneInterface** We can see that Chraracter Class has another class as its property. So when we have to clone Character we will have to also clone its property when it is a reference variable. ::: info ***Clone property class:*** We can see that in public Character clone(){}, we will be cloning the property that this class has. >this.abilities.clone() [color=blue] ::: ``` java== public class Character implements CloneInterface{ Abilities abilities; String name; String job; public Character(Abilities ability, String name, String job){ this.abilities = ability; this.name = name; this.job = job; } @Override public Character clone(){ return new Character(this.abilities.clone(), this.name, this.job); } @Override public String toString(){ return "This character is a " + this.job + " and its name is " + this.name + ". " + this.abilities.toString(); } } ``` **3. Abilities Class implementing CloneInterface** ``` java== public class Abilities implements CloneInterface{ String skill; int damage; public Abilities(String skill, int damage){ this.skill = skill; this.damage = damage; } @Override public Abilities clone(){ return new Abilities(this.skill, this.damage); } @Override public String toString(){ return "Its special skill is " + this.skill + " and overall damage is " + this.damage + "."; } } ``` ***The usage of the Deep Design Pattern:*** We can see below that the print in both are the same. ``` java== public static void main(String[] args) { Abilities abilities = new Abilities("Heal", 150); Character character = new Character(abilities, "Miyu", "Mage"); System.out.println(character.toString()); Character cloneCharacter = character.clone(); System.out.println(cloneCharacter.toString()); } ``` :::info ***Result:*** This character is a Mage and its name is Miyu. Its special skill is Heal and overall damage is 150. This character is a Mage and its name is Miyu. Its special skill is Heal and overall damage is 150. :::