refactoring : temporary field - introduce special case

This commit is contained in:
haerong22
2022-04-05 23:24:36 +09:00
parent 4321bf7ffe
commit fabf7d0255
15 changed files with 265 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class BasicBillingPlan extends BillingPlan {
}

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class BillingPlan {
}

View File

@@ -0,0 +1,44 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class Customer {
private String name;
private BillingPlan billingPlan;
private PaymentHistory paymentHistory;
public Customer(String name, BillingPlan billingPlan, PaymentHistory paymentHistory) {
this.name = name;
this.billingPlan = billingPlan;
this.paymentHistory = paymentHistory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BillingPlan getBillingPlan() {
return billingPlan;
}
public void setBillingPlan(BillingPlan billingPlan) {
this.billingPlan = billingPlan;
}
public PaymentHistory getPaymentHistory() {
return paymentHistory;
}
public void setPaymentHistory(PaymentHistory paymentHistory) {
this.paymentHistory = paymentHistory;
}
public boolean isUnknown() {
return false;
}
}

View File

@@ -0,0 +1,17 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class CustomerService {
public String customerName(Site site) {
return site.getCustomer().getName();
}
public BillingPlan billingPlan(Site site) {
return site.getCustomer().getBillingPlan();
}
public int weeksDelinquent(Site site) {
return site.getCustomer().getPaymentHistory().getWeeksDelinquentInLastYear();
}
}

View File

@@ -0,0 +1,7 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class NullPaymentHistory extends PaymentHistory {
public NullPaymentHistory() {
super(0);
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class PaymentHistory {
private int weeksDelinquentInLastYear;
public PaymentHistory(int weeksDelinquentInLastYear) {
this.weeksDelinquentInLastYear = weeksDelinquentInLastYear;
}
public int getWeeksDelinquentInLastYear() {
return this.weeksDelinquentInLastYear;
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class Site {
private Customer customer;
public Site(Customer customer) {
this.customer = customer.getName().equals("unknown") ? new UnknownCustomer() : customer;
}
public Customer getCustomer() {
return customer;
}
}

View File

@@ -0,0 +1,18 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
public class UnknownCustomer extends Customer {
public UnknownCustomer() {
super("unknown", new BasicBillingPlan(), new NullPaymentHistory());
}
@Override
public boolean isUnknown() {
return true;
}
@Override
public String getName() {
return "occupant";
}
}

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class BasicBillingPlan extends BillingPlan {
}

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class BillingPlan {
}

View File

@@ -0,0 +1,40 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class Customer {
private String name;
private BillingPlan billingPlan;
private PaymentHistory paymentHistory;
public Customer(String name, BillingPlan billingPlan, PaymentHistory paymentHistory) {
this.name = name;
this.billingPlan = billingPlan;
this.paymentHistory = paymentHistory;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BillingPlan getBillingPlan() {
return billingPlan;
}
public void setBillingPlan(BillingPlan billingPlan) {
this.billingPlan = billingPlan;
}
public PaymentHistory getPaymentHistory() {
return paymentHistory;
}
public void setPaymentHistory(PaymentHistory paymentHistory) {
this.paymentHistory = paymentHistory;
}
}

View File

@@ -0,0 +1,28 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class CustomerService {
public String customerName(Site site) {
Customer customer = site.getCustomer();
String customerName;
if (customer.getName().equals("unknown")) {
customerName = "occupant";
} else {
customerName = customer.getName();
}
return customerName;
}
public BillingPlan billingPlan(Site site) {
Customer customer = site.getCustomer();
return customer.getName().equals("unknown") ? new BasicBillingPlan() : customer.getBillingPlan();
}
public int weeksDelinquent(Site site) {
Customer customer = site.getCustomer();
return customer.getName().equals("unknown") ? 0 : customer.getPaymentHistory().getWeeksDelinquentInLastYear();
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class PaymentHistory {
private int weeksDelinquentInLastYear;
public PaymentHistory(int weeksDelinquentInLastYear) {
this.weeksDelinquentInLastYear = weeksDelinquentInLastYear;
}
public int getWeeksDelinquentInLastYear() {
return this.weeksDelinquentInLastYear;
}
}

View File

@@ -0,0 +1,14 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case._before;
public class Site {
private Customer customer;
public Site(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return customer;
}
}

View File

@@ -0,0 +1,39 @@
package com.example.refactoring._16_temporary_field._36_introduce_special_case;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CustomerServiceTest {
CustomerService customerService = new CustomerService();
Customer unknown = new Customer("unknown", null, null);
BillingPlan billingPlan = new BillingPlan();
Customer kim = new Customer("kim", billingPlan, new PaymentHistory(1));
@Test
void customerName() {
String unknownCustomerName = customerService.customerName(new Site(unknown));
assertEquals("occupant", unknownCustomerName);
String customer = customerService.customerName(new Site(kim));
assertEquals("kim", customer);
}
@Test
void billingPlan() {
assertTrue((customerService.billingPlan(new Site(unknown)) instanceof BasicBillingPlan));
assertEquals(billingPlan, customerService.billingPlan(new Site(kim)));
}
@Test
void weeksDelingquent() {
assertEquals(1, customerService.weeksDelinquent(new Site(kim)));
assertEquals(0, customerService.weeksDelinquent(new Site(unknown)));
}
}