Creational Design Patterns: Prototype Method
Purpose
The Prototype Method is a creational design pattern used when creating new objects is costly or complex, and we want to reuse existing objects as a starting point. Instead of building a new object from scratch every time, we take an existing object (the “prototype”) and clone it, making necessary adjustments along the way. This approach is efficient and flexible, especially when the object creation process is resource-intensive.
Explanation
Imagine you’re working on an application that creates vehicles (cars, bikes, etc.) with a lot of customizable options, like different engines, features, and types. Instead of repeatedly going through the setup process for every new vehicle, you can define a prototype vehicle and clone it to create new ones. This saves time and makes your code simpler, as you’re only copying an existing, predefined object and tweaking it if necessary.
Key Idea:
- You start with a prototype (a basic vehicle, for example) that’s already been set up with certain characteristics.
- When you need another similar object, you clone the prototype instead of making it from scratch.
- You can then modify the cloned object to suit your needs without affecting the original prototype.
Ruby Example: Vehicle Factory
Let’s say you have a car that’s already been configured with a powerful engine and luxury features, like a sunroof and leather seats. Instead of manually creating a new car every time, you can clone the existing one and make any small changes you need, such as adding heated seats to the cloned car.
Here’s how this would look in Ruby:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Define a Vehicle class with common properties
class Vehicle
attr_accessor :type, :engine, :wheels, :features
def initialize(type, engine, wheels, features = [])
@type = type
@engine = engine
@wheels = wheels
@features = features
end
# Cloning method to create a copy of the vehicle
def clone
dup # Creates a shallow copy of the object
end
def details
"Vehicle Type: #{@type}, Engine: #{@engine}, Wheels: #{@wheels}, Features: #{@features.join(', ')}"
end
end
# Creating a prototype car
prototype_car = Vehicle.new("Car", "V8 Engine", 4, ["Sunroof", "Leather Seats"])
# Cloning the car to make a new one with extra features
cloned_car = prototype_car.clone
cloned_car.features << "Heated Seats"
# Showing the details of both the prototype and cloned cars
puts "Original Car: #{prototype_car.details}"
puts "Cloned Car: #{cloned_car.details}"
Explanation:
- Prototype Car: We start with a basic car that has a V8 engine, four wheels, and features like a sunroof and leather seats.
- Cloning: When we want another car, we clone the prototype. The new car is a copy, but we can add or change its features (like adding heated seats) without changing the original.
Uses:
- Efficiency: Instead of recreating similar objects multiple times, you copy an existing one, which saves both time and resources.
- Customization: Each clone can be tweaked and customized independently.
- Flexibility: You can create different types of vehicles based on a common structure, like motorcycles or trucks, by cloning and making slight adjustments.
Conclusion:
The Prototype Method is about using what you already have, making a copy, and customizing it, which is especially useful when building new objects from scratch is too complex or time-consuming. In our vehicle example, it allows us to create new cars or motorcycles by simply cloning a prototype and adjusting the specifics without starting from zero.
Comments powered by Disqus.