Prototype in Ruby
Prototype is a creational design pattern that allows cloning objects, even complex ones, without coupling to their specific classes.
All prototype classes should have a common interface that makes it possible to copy objects even if their concrete classes are unknown. Prototype objects can produce full copies since objects of the same class can access each other’s private fields.
Learn more about Prototype
Usage of the pattern in Ruby
Usage examples: The Prototype pattern is available in Ruby out of the box with a
Identification: The prototype can be easily recognized by a
copy methods, etc.
This example illustrates the structure of the
Prototype design pattern. It focuses on answering these questions:
What classes does it consist of?
What roles do these classes play?
In what way the elements of the pattern are related?
main.rb: Conceptual Example
# The example class that has cloning ability. We'll see how the values of field
# with different types will be cloned.
attr_accessor :primitive, :component, :circular_reference
@primitive = nil
@component = nil
@circular_reference = nil
# @return [Prototype]
@component = deep_copy(@component)
# Cloning an object that has a nested object with backreference requires
# special treatment. After the cloning is completed, the nested object
# should point to the cloned object, instead of the original object.
@circular_reference = deep_copy(@circular_reference)
@circular_reference.prototype = self
# deep_copy is the usual Marshalling hack to make a deep copy. But it's rather
# slow and inefficient, therefore, in real applications, use a special gem
private def deep_copy(object)
# @param [Prototype] prototype
@prototype = prototype
# The client code.
p1 = Prototype.new
p1.primitive = 245
p1.component = Time.now
p1.circular_reference = ComponentWithBackReference.new(p1)
p2 = p1.clone
if p1.primitive == p2.primitive
puts 'Primitive field values have been carried over to a clone. Yay!'
puts 'Primitive field values have not been copied. Booo!'
puts 'Simple component has not been cloned. Booo!'
puts 'Simple component has been cloned. Yay!'
puts 'Component with back reference has not been cloned. Booo!'
puts 'Component with back reference has been cloned. Yay!'
print 'Component with back reference is linked to original object. Booo!'
print 'Component with back reference is linked to the clone. Yay!'
output.txt: Execution result
Primitive field values have been carried over to a clone. Yay!
Simple component has been cloned. Yay!
Component with back reference has been cloned. Yay!
Component with back reference is linked to the clone. Yay!
Prototype in Other Languages