Skip to content

Instantly share code, notes, and snippets.

@cheeyeo
Created April 14, 2014 19:37
Show Gist options
  • Save cheeyeo/10676940 to your computer and use it in GitHub Desktop.
Save cheeyeo/10676940 to your computer and use it in GitHub Desktop.
Multion pattern
# Multiton pattern
# extension to the Singleton pattern which maps unique keys to specific instances of a class. The idea is that there should only be one instance of an object for each unique key in use, limiting the number of objects that need to be created
class Font
class << self
def file_names
@file_names ||= {}
end
def instances
@instances ||= {}
end
def map(params)
file_names.update(params)
end
def [](name)
instances[name] ||= new(file_names[name])
end
end
def initialize(filename)
puts "Processing #{filename}"
end
end
Font.map("Times New Roman" => "/path/to/times.ttf")
Font["Times New Roman"] #=> A font instance
p Font.file_names.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment