Patterns / Creational patterns
image

# Prototype
class Shape
    attr_reader :line_count

    def initialize(line_count)
        @line_count = line_count
    end

    def clone
        Shape.new(@line_count)
    end
end

# ConcretePrototype
class Square < Shape
    def initialize
        super(4)
    end
end

# Client
class ShapeMaker
    def initialize(shape)
        @shape = shape
    end

    def make_shape()
        return @shape.clone()
    end
end

square = Square.new
maker = ShapeMaker.new(square)

square1 = maker.make_shape()
square2 = maker.make_shape()

puts square1 == square2
puts square1.line_count


Specify the kinds of objects to create using a prototypical instance, and create new objectsby copying this prototype.