Prototype Pattern
Description:
The Prototype Pattern is a creational design pattern that allows you to create new objects by copying an existing object, known as the “prototype.” It is particularly useful when the cost of creating an object is more expensive or complex than copying it, or when you want to create new objects with the same initial state as an existing one.
How it’s Used:
- Prototype Interface or Abstract Class: Define a common interface or abstract class for all concrete prototypes. This interface should include a method for cloning the object.
- Concrete Prototypes: Create concrete classes that implement the prototype interface or extend the prototype abstract class. These classes define the specific properties and behaviors of the objects you want to clone.
- Clone Method: Implement the clone method in each concrete prototype class. This method should create a new instance of the same class and copy the state from the current instance to the new one.
- Client Code: In your application, use the prototype to create new objects by cloning the prototype object. The client code should not be responsible for creating objects from scratch but should rely on the prototype to provide new instances.
Example:
Let’s say you are developing a game where characters can be customized with various attributes, such as name, appearance, and abilities. Instead of creating each character from scratch, you can use the Prototype Pattern to clone a prototype character with default attributes and customize the clones as needed:
- Prototype Interface or Abstract Class: Define a
Characterinterface or abstract class that includes aclonemethod.
public interface Character {
Character clone();
void customize(String name, String appearance, String abilities);
void displayInfo();
}- Concrete Prototypes: Create concrete character classes that implement the
Characterinterface or extend theCharacterabstract class. Each class represents a specific type of character.
public class Warrior implements Character {
private String name;
private String appearance;
private String abilities;
public Warrior() {
// Default attributes for a warrior
this.name = "Warrior";
this.appearance = "Strong and armored";
this.abilities = "Melee combat skills";
}
@Override
public Character clone() {
Warrior clone = new Warrior();
clone.customize(this.name, this.appearance, this.abilities);
return clone;
}
@Override
public void customize(String name, String appearance, String abilities) {
this.name = name;
this.appearance = appearance;
this.abilities = abilities;
}
@Override
public void displayInfo() {
System.out.println("Character: " + name);
System.out.println("Appearance: " + appearance);
System.out.println("Abilities: " + abilities);
}
}
// Create similar classes for other character types (e.g., Mage, Rogue)- Client Code: In your game code, use the prototype to create new character instances and customize them as needed:
Character prototype = new Warrior();
Character warrior1 = prototype.clone();
warrior1.customize("Sir Lancelot", "Shiny armor", "Swordsmanship");
Character warrior2 = prototype.clone();
warrior2.customize("Lady Guinevere", "Elegant armor", "Archery");
warrior1.displayInfo();
warrior2.displayInfo();By using the Prototype Pattern, you can create new character instances by cloning the prototype, customizing them for different characters in your game, and displaying their individual attributes. This pattern promotes code reusability and flexibility when creating objects with similar initial states.