Created
September 14, 2012 23:13
-
-
Save myronmarston/3725544 to your computer and use it in GitHub Desktop.
Alternate pattern for Shock costing from Sandi Metz's GoGaRuCo Talk
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
class Shock | |
def intialize(type) | |
@type = type | |
end | |
def cost | |
coster_for(type).compute | |
end | |
private | |
def coster_for(type) | |
coster_implementations.fetch(type) do | |
raise ArgumentError, "Unrecoganized cost type: #{type}" | |
end.new | |
end | |
def self.coster_implementations | |
@coster_implementations ||= {} | |
end | |
class BaseCoster | |
def self.register_as(type) | |
Shock.coster_implementations[type] = self | |
end | |
end | |
class FrontShockCoster < BaseCoster | |
def compute | |
end | |
register_as :front | |
end | |
class RearShockCoster < BaseCoster | |
def compute | |
end | |
register_as :rear | |
end | |
class LeftyShockCoster < BaseCoster | |
def compute | |
end | |
register_as :lefty | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment