refactoring : mutable data - separate query from modifier
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
public class Billing {
|
||||
|
||||
private Customer customer;
|
||||
|
||||
private EmailGateway emailGateway;
|
||||
|
||||
public Billing(Customer customer, EmailGateway emailGateway) {
|
||||
this.customer = customer;
|
||||
this.emailGateway = emailGateway;
|
||||
}
|
||||
|
||||
// 어떤 값을 조회하는 메소드에서는 사이드이펙트가 생기지 않도록 조회와 명령을 분리한다.
|
||||
public double getTotalOutstanding() {
|
||||
return customer.getInvoices().stream()
|
||||
.map(Invoice::getAmount)
|
||||
.reduce((double) 0, Double::sum);
|
||||
}
|
||||
|
||||
public void sendBill() {
|
||||
emailGateway.send(formatBill(customer));
|
||||
}
|
||||
|
||||
private String formatBill(Customer customer) {
|
||||
return "sending bill for " + customer.getName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Criminal {
|
||||
|
||||
public void alertForMiscreant(List<Person> people) {
|
||||
if (!findMiscreant(people).isBlank()) {
|
||||
setOffAlarms();
|
||||
}
|
||||
}
|
||||
|
||||
public String findMiscreant(List<Person> people) {
|
||||
for (Person p : people) {
|
||||
if (p.getName().equals("Don")) {
|
||||
return "Don";
|
||||
}
|
||||
|
||||
if (p.getName().equals("John")) {
|
||||
return "John";
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private void setOffAlarms() {
|
||||
System.out.println("set off alarm");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String name;
|
||||
|
||||
private List<Invoice> invoices;
|
||||
|
||||
public Customer(String name, List<Invoice> invoices) {
|
||||
this.name = name;
|
||||
this.invoices = invoices;
|
||||
}
|
||||
|
||||
public List<Invoice> getInvoices() {
|
||||
return invoices;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
public class EmailGateway {
|
||||
public void send(String bill) {
|
||||
System.out.println(bill);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
private double amount;
|
||||
|
||||
public Invoice(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
public class Billing {
|
||||
|
||||
private Customer customer;
|
||||
|
||||
private EmailGateway emailGateway;
|
||||
|
||||
public Billing(Customer customer, EmailGateway emailGateway) {
|
||||
this.customer = customer;
|
||||
this.emailGateway = emailGateway;
|
||||
}
|
||||
|
||||
public double getTotalOutstandingAndSendBill() {
|
||||
double result = customer.getInvoices().stream()
|
||||
.map(Invoice::getAmount)
|
||||
.reduce((double) 0, Double::sum);
|
||||
sendBill();
|
||||
return result;
|
||||
}
|
||||
|
||||
private void sendBill() {
|
||||
emailGateway.send(formatBill(customer));
|
||||
}
|
||||
|
||||
private String formatBill(Customer customer) {
|
||||
return "sending bill for " + customer.getName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Criminal {
|
||||
|
||||
public String alertForMiscreant(List<Person> people) {
|
||||
for (Person p : people) {
|
||||
if (p.getName().equals("Don")) {
|
||||
setOffAlarms();
|
||||
return "Don";
|
||||
}
|
||||
|
||||
if (p.getName().equals("John")) {
|
||||
setOffAlarms();
|
||||
return "John";
|
||||
}
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private void setOffAlarms() {
|
||||
System.out.println("set off alarm");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String name;
|
||||
|
||||
private List<Invoice> invoices;
|
||||
|
||||
public Customer(String name, List<Invoice> invoices) {
|
||||
this.name = name;
|
||||
this.invoices = invoices;
|
||||
}
|
||||
|
||||
public List<Invoice> getInvoices() {
|
||||
return invoices;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
public class EmailGateway {
|
||||
public void send(String bill) {
|
||||
System.out.println(bill);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
private double amount;
|
||||
|
||||
public Invoice(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier.before;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BillingTest {
|
||||
|
||||
@Test
|
||||
void totalOutstanding() {
|
||||
Billing billing = new Billing(new Customer("kim", List.of(new Invoice(20), new Invoice(30))),
|
||||
new EmailGateway());
|
||||
assertEquals(50d, billing.getTotalOutstanding());
|
||||
|
||||
billing.sendBill();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.refactoring._06_mutable_data._19_separate_query_from_modifier;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CriminalTest {
|
||||
|
||||
@Test
|
||||
void alertForMiscreant() {
|
||||
Criminal criminal = new Criminal();
|
||||
String found = criminal.findMiscreant(List.of(new Person("kim"), new Person("Don")));
|
||||
assertEquals("Don", found);
|
||||
|
||||
found = criminal.findMiscreant(List.of(new Person("John"), new Person("Don")));
|
||||
assertEquals("John", found);
|
||||
|
||||
found = criminal.findMiscreant(List.of(new Person("kim"), new Person("Jane")));
|
||||
assertEquals("", found);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user