Created
April 14, 2014 19:37
-
-
Save cheeyeo/10676940 to your computer and use it in GitHub Desktop.
Multion pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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