From 3b457c17e7a8fdbff498e2327f0079b58461311f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Mota?= Date: Fri, 3 May 2013 16:05:19 +0100 Subject: [PATCH] Replace Type Code with Polymorphism. --- README.md | 38 +++++++++---------- .../lib/after.rb | 33 ++++++++++++++++ .../lib/before.rb | 28 ++++++++++++++ .../test/test.rb | 19 ++++++++++ 4 files changed, 98 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index fb64e7d..c53541c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/replace-type-code-with-polymorphism/lib/after.rb b/replace-type-code-with-polymorphism/lib/after.rb index e69de29..20542ff 100644 --- a/replace-type-code-with-polymorphism/lib/after.rb +++ b/replace-type-code-with-polymorphism/lib/after.rb @@ -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 diff --git a/replace-type-code-with-polymorphism/lib/before.rb b/replace-type-code-with-polymorphism/lib/before.rb index e69de29..2c37571 100644 --- a/replace-type-code-with-polymorphism/lib/before.rb +++ b/replace-type-code-with-polymorphism/lib/before.rb @@ -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 diff --git a/replace-type-code-with-polymorphism/test/test.rb b/replace-type-code-with-polymorphism/test/test.rb index a8f6974..4b3a8ff 100644 --- a/replace-type-code-with-polymorphism/test/test.rb +++ b/replace-type-code-with-polymorphism/test/test.rb @@ -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