-
-
Save jordanbyron/607916 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
## Form Template Method (http://refactoring.com/catalog/formTemplateMethod.html) | |
class Site | |
end | |
class ResidentialSite < Site | |
def initialize | |
@units = 1.0 | |
@rate = 2.0 | |
end | |
def billable_amount | |
base = @units * @rate * 0.5 | |
tax = base * 0.2 | |
base + tax | |
end | |
end | |
class LifelineSite < Site | |
def initialize | |
@units = 2.0 | |
@rate = 2.2 | |
end | |
def billable_amount | |
base = @units * @rate * 0.5 | |
tax = base * 0.2 | |
base + tax | |
end | |
end | |
# Refactored | |
module Site | |
def base_amount | |
@units * @rate * 0.5 | |
end | |
def tax_amount | |
base_amount * 0.2 | |
end | |
def billable_amount | |
base_amount + tax_amount | |
end | |
end | |
class ResidentialSite | |
include Site | |
def initialize | |
@units = 2.0 | |
@rate = 2.2 | |
end | |
end | |
class LifelineSite | |
include Site | |
def initialize | |
@units = 1.0 | |
@rate = 2.0 | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment