forked from tutsplus/ruby-refactoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Type Code with Polymorphism.
- Loading branch information
1 parent
0285023
commit 3b457c1
Showing
4 changed files
with
98 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file contains 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
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 |
This file contains 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