Last active
September 21, 2017 17:31
-
-
Save DanielBlanco/d57c7dec0ff10d9641570f7ea2374ff8 to your computer and use it in GitHub Desktop.
Replace Conditional with Polymorphism
This file contains 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 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 |
This file contains 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
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