Skip to content

Instantly share code, notes, and snippets.

@DanielBlanco
Last active September 21, 2017 17:31
Show Gist options
  • Save DanielBlanco/d57c7dec0ff10d9641570f7ea2374ff8 to your computer and use it in GitHub Desktop.
Save DanielBlanco/d57c7dec0ff10d9641570f7ea2374ff8 to your computer and use it in GitHub Desktop.
Replace Conditional with Polymorphism
class MountainBike
def price
case @type_code
when :rigid
(1 + @commission) * @base_price
when :front_suspension
(1 + @commission) * @base_price + @front_suspension_price
when :full_suspension
(1 + @commission) * @base_price + @front_suspension_price +
@rear_suspension_price
end
end
end
module MountainBike
...
end
class RigidMountainBike
include MountainBike
def price
(1 + @commission) * @base_price
end
end
class FrontSuspensionMountainBike
include MountainBike
def price
(1 + @commission) * @base_price + @front_suspension_price
end
end
class FullSuspensionMountainBike
include MountainBike
def price
(1 + @commission) * @base_price + @front_suspension_price +
@rear_suspension_price
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment