
# Subject
class Graphic
def initialize(file_name)
@file_name = file_name
end
def get_file_name()
return @file_name
end
end
# RealSubject
class Image < Graphic
def initialize(file_name)
super(file_name)
end
# Request()
def draw()
puts "draw " + @file_name
end
end
# Proxy
class ImageProxy < Graphic
def initialize(file_name)
super(file_name)
@image = nil
end
def draw()
get_image().draw()
end
def get_image()
if @image == nil
@image = Image.new(@file_name)
end
return @image
end
end
# Client
proxy = ImageProxy.new("1.png")
# operation without creating a RealSubject
fileName = proxy.get_file_name()
# forwarded to the RealSubject
proxy.draw()
puts "fileName is #{fileName}"