refactoring : mutable data - combine functions into transform
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
public class Client1 extends ReadingClient {
|
||||
|
||||
double baseCharge;
|
||||
|
||||
public Client1(Reading reading) {
|
||||
this.baseCharge = enrichReading(reading).baseCharge();
|
||||
}
|
||||
|
||||
public double getBaseCharge() {
|
||||
return baseCharge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
public class Client2 extends ReadingClient {
|
||||
|
||||
private double base;
|
||||
private double taxableCharge;
|
||||
|
||||
public Client2(Reading reading) {
|
||||
EnrichReading enrichReading = enrichReading(reading);
|
||||
this.base = enrichReading.baseCharge();
|
||||
this.taxableCharge = enrichReading.taxableCharge();
|
||||
}
|
||||
|
||||
public double getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
public double getTaxableCharge() {
|
||||
return taxableCharge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
public class Client3 extends ReadingClient{
|
||||
|
||||
private double basicChargeAmount;
|
||||
|
||||
public Client3(Reading reading) {
|
||||
this.basicChargeAmount = enrichReading(reading).baseCharge();
|
||||
}
|
||||
|
||||
public double getBasicChargeAmount() {
|
||||
return basicChargeAmount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
public record EnrichReading(Reading reading, double baseCharge, double taxableCharge) {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public record Reading(String customer, double quantity, Month month, Year year) {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public class ReadingClient {
|
||||
|
||||
protected double taxThreshold(Year year) {
|
||||
return 5;
|
||||
}
|
||||
|
||||
protected double baseRate(Month month, Year year) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
protected EnrichReading enrichReading(Reading reading) {
|
||||
return new EnrichReading(reading, baseCharge(reading), taxableCharge(reading));
|
||||
}
|
||||
|
||||
private double taxableCharge(Reading reading) {
|
||||
return Math.max(0, baseCharge(reading) - taxThreshold(reading.year()));
|
||||
}
|
||||
|
||||
private double baseCharge(Reading reading) {
|
||||
return baseRate(reading.month(), reading.year()) * reading.quantity();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform.before;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public class Client1 {
|
||||
|
||||
double baseCharge;
|
||||
|
||||
public Client1(Reading reading) {
|
||||
this.baseCharge = baseRate(reading.month(), reading.year()) * reading.quantity();
|
||||
}
|
||||
|
||||
private double baseRate(Month month, Year year) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public double getBaseCharge() {
|
||||
return baseCharge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform.before;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public class Client2 {
|
||||
|
||||
private double base;
|
||||
private double taxableCharge;
|
||||
|
||||
public Client2(Reading reading) {
|
||||
this.base = baseRate(reading.month(), reading.year()) * reading.quantity();
|
||||
this.taxableCharge = Math.max(0, this.base - taxThreshold(reading.year()));
|
||||
}
|
||||
|
||||
private double taxThreshold(Year year) {
|
||||
return 5;
|
||||
}
|
||||
|
||||
private double baseRate(Month month, Year year) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public double getBase() {
|
||||
return base;
|
||||
}
|
||||
|
||||
public double getTaxableCharge() {
|
||||
return taxableCharge;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform.before;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public class Client3 {
|
||||
|
||||
private double basicChargeAmount;
|
||||
|
||||
public Client3(Reading reading) {
|
||||
this.basicChargeAmount = calculateBaseCharge(reading);
|
||||
}
|
||||
|
||||
private double calculateBaseCharge(Reading reading) {
|
||||
return baseRate(reading.month(), reading.year()) * reading.quantity();
|
||||
}
|
||||
|
||||
private double baseRate(Month month, Year year) {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public double getBasicChargeAmount() {
|
||||
return basicChargeAmount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform.before;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
public record Reading(String customer, double quantity, Month month, Year year) {
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.refactoring._06_mutable_data._22_combine_functions_into_transform;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Month;
|
||||
import java.time.Year;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ReadingClientTest {
|
||||
|
||||
@Test
|
||||
void client1() {
|
||||
Client1 client1 = new Client1(acquireReading());
|
||||
assertEquals(100d, client1.getBaseCharge());
|
||||
}
|
||||
|
||||
@Test
|
||||
void client2() {
|
||||
Client2 client2 = new Client2(acquireReading());
|
||||
assertEquals(100d, client2.getBase());
|
||||
assertEquals(95d, client2.getTaxableCharge());
|
||||
}
|
||||
|
||||
@Test
|
||||
void client3() {
|
||||
Client3 client3 = new Client3(acquireReading());
|
||||
assertEquals(100d, client3.getBasicChargeAmount());
|
||||
}
|
||||
|
||||
|
||||
private Reading acquireReading() {
|
||||
return new Reading("kim", 10, Month.MARCH, Year.of(2022));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user