Skip to content

Commit

Permalink
Replace Type Code with Polymorphism.
Browse files Browse the repository at this point in the history
  • Loading branch information
josemotanet committed Jun 4, 2013
1 parent 0285023 commit 3b457c1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 20 deletions.
38 changes: 18 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,21 @@ possible refactoring patterns to solve it.

### Refactoring Patterns

- Extract Class
- Extract Method
- Move Method
- Form Template Method
- Inline Class
- Introduce Assertion
- Introduce Null Object
- Introduce Parameter Object
- Preserve Whole Object
- Move Method
- Move Field
- Pull Up Method
- Rename Method
- Replace Array with Object
- Replace Method with Method Object
- Replace Temp with Query
- Replace Temp with Chain
- Replace Type Code with Polymorphism
- Replace Type Code with Module Extension
- Replace Type Code with State/Strategy
* [x] Extract Class
* [x] Extract Method
* [x] Form Template Method
* [x] Hide Delegate
* [x] Introduce Assertion
* [x] Introduce Null Object
* [x] Separate Query from Modifier
* [x] Move Field
* [x] Parameterize Method
* [x] Preserve Whole Object
* [x] Pull Up Method
* [x] Rename Method
* [x] Replace Array with Object
* [x] Replace Method with Method Object
* [x] Replace Temp with Query
* [-] Replace Type Code with Module Extension
* [x] Replace Type Code with Polymorphism
* [-] Replace Type Code with State/Strategy
33 changes: 33 additions & 0 deletions replace-type-code-with-polymorphism/lib/after.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Employee
def initialize(type: :regular)
@type = type
end

def base_salary
500.0
end

def salary
base_salary + bonus
end

def self.build(type: :employee)
const_get(type.capitalize).new
end

def bonus
0
end
end

class Manager < Employee
def bonus
800
end
end

class Boss < Employee
def bonus
1500
end
end
28 changes: 28 additions & 0 deletions replace-type-code-with-polymorphism/lib/before.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Employee
def initialize(type: :regular)
@type = type
end

def base_salary
500.0
end

def salary
base_salary + bonus
end

def self.build(type: :regular)
new type: type
end

private

def bonus
value = case @type
when :regular then 0
when :boss then 1500.0
when :manager then 800.0
end
end

end
19 changes: 19 additions & 0 deletions replace-type-code-with-polymorphism/test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,22 @@

require 'before' if ENV["BEFORE"]
require 'after' unless ENV["BEFORE"]

describe Employee do
describe "a regular one" do
it "has a salary" do
Employee.build.salary.must_equal 500.0
end
end

describe "a boss" do
it "has a salary" do
Employee.build(type: :boss).salary.must_equal 2000.0
end
end
describe "a manager" do
it "has a salary" do
Employee.build(type: :manager).salary.must_equal 1300.0
end
end
end

0 comments on commit 3b457c1

Please sign in to comment.