Last active
August 29, 2015 13:57
-
-
Save jsuchal/9421444 to your computer and use it in GitHub Desktop.
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
| # 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