Skip to content

Instantly share code, notes, and snippets.

@lucasprag
Created May 26, 2016 15:39
Show Gist options
  • Save lucasprag/ddd6739d9c981e08afc93a4a2f4850d0 to your computer and use it in GitHub Desktop.
Save lucasprag/ddd6739d9c981e08afc93a4a2f4850d0 to your computer and use it in GitHub Desktop.
key chain with polymorphism and single responsibility
class BedroomKeyGenerator
def self.generate
"algorithm of bedroom key" * 3
end
end
class FrontDoorKeyGenerator
def self.generate
"algorithm of front_door key" * 3
end
end
class BathroomKeyGenerator
def self.generate
"algorithm of bathroom key" * 3
end
end
class KeyChain
def initialize(door)
@door = door
end
def get
{
bedroom: BedroomKeyGenerator,
front_door: FrontDoorKeyGenerator,
bathroom: BathroomKeyGenerator
}.fetch(door).generate
end
private
attr_reader :door
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment