Welcome back to our journey through Creational Design Patterns. Previously, we explored the Builder Pattern, which allowed us to construct complex objects in a step-by-step manner. Now, let's dive into the Prototype Pattern. This pattern is particularly useful when you need to create new objects by copying existing ones.
In this lesson, you'll discover how to utilize the Prototype Pattern to create new objects by cloning existing ones. Specifically, you will learn:
Vehicle
class and its Car
subclass.For example, if you have a predefined Car
object with specific attributes like model and engine type, you can easily create another Car
object with the same attributes using the Prototype Pattern. This can save you time and ensure consistency when creating similar objects.
The Prototype Pattern also falls under the category of Creational Design Patterns and is centered on the concept of cloning objects. Instead of instantiating new objects directly, the Prototype Pattern relies on the clone method to create copies of existing objects. This pattern is particularly useful in scenarios where object creation is resource-intensive or complex, allowing for efficient and consistent replication of objects.
Let's break down the implementation of the Prototype Pattern using a Vehicle class and its Car subclass. Imagine you are in a car manufacturing system where each car's model and engine type must be accurately replicated for quality and efficiency. Using the Prototype Pattern, you can quickly and reliably produce new car instances, reducing risk and boosting productivity.
First, create an interface that declares the clone
method:
Java1public interface Prototype { 2 Prototype clone(); 3}
This interface defines the contract for cloning objects. Any class that implements this interface must provide an implementation of the clone
method.
Next, implement the Vehicle
class that includes basic properties and implements the Prototype
interface:
Java1public abstract class Vehicle implements Prototype { 2 private String model; 3 4 public Vehicle(String model) { 5 this.model = model; 6 } 7 8 public String getModel() { 9 return model; 10 } 11 12 @Override 13 public abstract Vehicle clone(); 14}
The Vehicle
class stores a common property (model
) and implements the clone
method from the Prototype
interface. Since Vehicle
is abstract, its subclasses will provide concrete implementations of the clone
method.
Now, create the Car
subclass that extends Vehicle
:
Java1public class Car extends Vehicle { 2 private String engineType; 3 4 public Car(String model, String engineType) { 5 super(model); 6 this.engineType = engineType; 7 } 8 9 public String getEngineType() { 10 return engineType; 11 } 12 13 @Override 14 public Car clone() { 15 return new Car(this.getModel(), this.engineType); 16 } 17}
The Car
class extends Vehicle
and adds an additional property (engineType
). The clone
method creates a new Car
object with the same model
and engineType
as the original.
Finally, test the cloning process in a Main
class:
Java1public class Main { 2 public static void main(String[] args) { 3 Car originalCar = new Car("Model S", "Electric"); 4 Car clonedCar = originalCar.clone(); 5 6 System.out.println("Original Car: " + originalCar.getModel() + " - " + originalCar.getEngineType()); 7 System.out.println("Cloned Car: " + clonedCar.getModel() + " - " + clonedCar.getEngineType()); 8 } 9}
The Main
class tests the implementation by creating an original Car
object and then cloning it. The output shows that the cloned Car
has the same properties as the original.
The Prototype Pattern is important because it simplifies object creation, especially when object instantiation is more expensive or complex. By cloning an existing object, you can:
Imagine you are in a car manufacturing system where each car's model and engine type must be accurately replicated for quality and efficiency. Using the Prototype Pattern, you can quickly and reliably produce new car instances, reducing risk and boosting productivity.
Excited to see this in action? Let's head into the practice section and start cloning!