Skip to content

Instantly share code, notes, and snippets.

@jsuchal
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save jsuchal/9421444 to your computer and use it in GitHub Desktop.

Select an option

Save jsuchal/9421444 to your computer and use it in GitHub Desktop.
# sandi
module GildedRose
DEFAULT_CLASS = Item
SPECIALIZED_CLASSES = {
'normal' => Normal,
'Aged Brie' => Brie,
'Backstage passes to ...' => Backstage
}
def self.for(name, quality, days_remaining)
(SPECIALIZED_CLASSES[name] || DEFAULT_CLASS).new(quality, days_remaining)
end
end
# open for extension for configuration
class GildedRoseFactory < Struct.new(:specialized_classes, :default_class)
def for(name, quality, days_remaining)
specialized_classes.fetch(name, default_class).new(quality, days_remaining)
end
end
# usage
rose = GildedRoseFactory.new({
'normal' => Normal,
'Aged Brie' => Brie,
'Backstage passes to ...' => Backstage
}, Item)
rose.for #...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment