Created
September 9, 2013 18:39
-
-
Save djbender/6499724 to your computer and use it in GitHub Desktop.
a better polymorphic refactor for http://blog.8thlight.com/wai-lee-chin-feman/2013/08/11/anti-anti-if.html
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 Employee | |
attr_accessor :title, :name | |
def initialize(employee_attributes) | |
@title = employee_attributes[:title] | |
@name = employee_attributes[:name] | |
end | |
end | |
class Manager < Employee | |
def hours | |
40 | |
end | |
end | |
class Programmer < Employee | |
def hours | |
80 | |
end | |
end | |
class Thank | |
def generate(employee_attributes) | |
employee = EmployeeFactory.new_employee(employee_attributes) | |
"Thank you #{employee.name} for your #{employee.hours} hours of hard work!" | |
end | |
end | |
class EmployeeFactory | |
def self.new_employee(employee_attributes) | |
employee_attributes.title.constantize.new(employee_attributes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the feedback :-)
I think this only works in Rails.
In the blog, I was trying to make observations about OO in general; I just happened to be using Ruby.