refactoring : primitive obsession - replace type code with subclasses
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance;
|
||||
|
||||
public abstract class Employee {
|
||||
|
||||
private String name;
|
||||
|
||||
protected Employee(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static Employee createEmployee(String name, String type) {
|
||||
return switch (type) {
|
||||
case "engineer" -> new Engineer(name);
|
||||
case "manager" -> new Manager(name);
|
||||
case "salesman" -> new Salesman(name);
|
||||
default -> throw new IllegalArgumentException(type);
|
||||
};
|
||||
}
|
||||
|
||||
protected abstract String getType();
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"name='" + name + '\'' +
|
||||
", type='" + getType() + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance;
|
||||
|
||||
public class Engineer extends Employee {
|
||||
|
||||
public Engineer(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "engineer";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance;
|
||||
|
||||
public class Manager extends Employee {
|
||||
|
||||
public Manager(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "manager";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance;
|
||||
|
||||
public class Salesman extends Employee {
|
||||
|
||||
public Salesman(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return "salesman";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance._before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
public Employee(String name, String type) {
|
||||
this.validate(type);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private void validate(String type) {
|
||||
List<String> legalTypes = List.of("engineer", "manager", "salesman");
|
||||
if (!legalTypes.contains(type)) {
|
||||
throw new IllegalArgumentException(type);
|
||||
}
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"name='" + name + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
|
||||
private EmployeeType type;
|
||||
|
||||
public Employee(String name, String type) {
|
||||
this.name = name;
|
||||
this.type = this.employeeType(type);
|
||||
}
|
||||
|
||||
private EmployeeType employeeType(String type) {
|
||||
return switch (type) {
|
||||
case "engineer" -> new Engineer();
|
||||
case "manager" -> new Manager();
|
||||
case "salesman" -> new Salesman();
|
||||
default -> throw new IllegalArgumentException(type);
|
||||
};
|
||||
}
|
||||
|
||||
public String capitalizedType() {
|
||||
return this.type.capitalizedType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"name='" + name + '\'' +
|
||||
", type='" + type.toString() + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class EmployeeType {
|
||||
public String capitalizedType() {
|
||||
return this.toString().substring(0, 1).toUpperCase() + this.toString().substring(1).toLowerCase();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class Engineer extends EmployeeType {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "engineer";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class FullTimeEmployee extends Employee {
|
||||
public FullTimeEmployee(String name, String type) {
|
||||
super(name, type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class Manager extends EmployeeType {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "manager";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class PartTimeEmployee extends Employee {
|
||||
public PartTimeEmployee(String name, String type) {
|
||||
super(name, type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
public class Salesman extends EmployeeType {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "salesman";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance._before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
|
||||
public Employee(String name, String type) {
|
||||
this.validate(type);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
private void validate(String type) {
|
||||
List<String> legalTypes = List.of("engineer", "manager", "salesman");
|
||||
if (!legalTypes.contains(type)) {
|
||||
throw new IllegalArgumentException(type);
|
||||
}
|
||||
}
|
||||
|
||||
public String capitalizedType() {
|
||||
return this.type.substring(0, 1).toUpperCase() + this.type.substring(1).toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"name='" + name + '\'' +
|
||||
", type='" + type + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance._before;
|
||||
|
||||
public class FullTimeEmployee extends Employee {
|
||||
public FullTimeEmployee(String name, String type) {
|
||||
super(name, type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance._before;
|
||||
|
||||
public class PartTimeEmployee extends Employee {
|
||||
public PartTimeEmployee(String name, String type) {
|
||||
super(name, type);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.direct_inheritance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmployeeTest {
|
||||
|
||||
@Test
|
||||
void employeeType() {
|
||||
assertEquals("engineer", Employee.createEmployee("kim", "engineer").getType());
|
||||
assertEquals("manager", Employee.createEmployee("lee", "manager").getType());
|
||||
assertThrows(IllegalArgumentException.class, () -> Employee.createEmployee("park", "wrong type"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.refactoring._11_primitive_obsession._31_replace_type_code_with_subclasses.indirect_inheritance;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class EmployeeTest {
|
||||
|
||||
@Test
|
||||
void capitalizedType() {
|
||||
assertEquals("Manager", new FullTimeEmployee("kim", "manager").capitalizedType());
|
||||
assertEquals("Engineer", new PartTimeEmployee("lee", "engineer").capitalizedType());
|
||||
assertThrows(IllegalArgumentException.class, () -> new Employee("park", "wrong type"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user