Skip to content

Instantly share code, notes, and snippets.

@jordanbyron
Forked from practicingruby/refactoring patterns
Created October 2, 2010 19:34
Show Gist options
  • Save jordanbyron/607916 to your computer and use it in GitHub Desktop.
Save jordanbyron/607916 to your computer and use it in GitHub Desktop.
## 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