Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Flyweight
- class BigChar
- def initialize(char)
- @big_char = "#{char} expended a lot of memory"
- end
- def to_s
- "#{@big_char}"
- end
- end
- # FlyweightFactory
- class BigCharFactory
- def initialize
- @pool = {}
- end
- # singleton
- @singleton = new
- class << self
- def get_instance
- @singleton
- end
- end
- private_class_method :new
- def get_big_char(char)
- return @pool[char] if @pool.key?(char)
- @pool[char] = BigChar.new(char)
- @pool[char]
- end
- end
- # Client
- class BigString
- def initialize(string)
- @big_chars = []
- @big_char_factory = BigCharFactory.get_instance
- string.each_char {|c| @big_chars << @big_char_factory.get_big_char(c) }
- end
- def print
- @big_chars.each {|bc| puts(bc) }
- end
- end
- # main
- big_string = BigString.new('12345')
- big_string.print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement