refactoring : mutable data - split variable
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
public class Haggis {
|
||||
|
||||
private double primaryForce;
|
||||
private double secondaryForce;
|
||||
private double mass;
|
||||
private int delay;
|
||||
|
||||
public Haggis(double primaryForce, double secondaryForce, double mass, int delay) {
|
||||
this.primaryForce = primaryForce;
|
||||
this.secondaryForce = secondaryForce;
|
||||
this.mass = mass;
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
public double distanceTravelled(int time) {
|
||||
double result;
|
||||
// 가독성을 위해 변수의 이름을 역할에 맞도록 변경 변수의 값이 변경되지 않도록 final 키워드 사용
|
||||
final double primaryAcceleration = primaryForce / mass;
|
||||
int primaryTime = Math.min(time, delay);
|
||||
result = 0.5 * primaryAcceleration * primaryTime * primaryTime;
|
||||
|
||||
int secondaryTime = time - delay;
|
||||
if (secondaryTime > 0) {
|
||||
final double primaryVelocity = primaryAcceleration * delay;
|
||||
final double secondaryAcceleration = (primaryForce + secondaryForce) / mass;
|
||||
result += primaryVelocity * secondaryTime + 0.5 * secondaryAcceleration * secondaryTime + secondaryTime;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
public class Order {
|
||||
|
||||
public double discount(double inputValue, int quantity) {
|
||||
double result = inputValue;
|
||||
|
||||
if (inputValue > 50) result -= 2;
|
||||
if (quantity > 100) result -= 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
public class Rectangle {
|
||||
|
||||
private double perimeter;
|
||||
private double area;
|
||||
|
||||
public void updateGeometry(double height, double width) {
|
||||
// 가독성을 위해 변수의 이름을 역할에 맞도록 변경 변수의 값이 변경되지 않도록 final 키워드 사용
|
||||
final double perimeter = 2 * (height + width);
|
||||
System.out.println("Perimeter: " + perimeter);
|
||||
this.perimeter = perimeter;
|
||||
|
||||
final double area = height * width;
|
||||
System.out.println("Area: " + area);
|
||||
this.area = area;
|
||||
}
|
||||
|
||||
public double getPerimeter() {
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable.before;
|
||||
|
||||
public class Haggis {
|
||||
|
||||
private double primaryForce;
|
||||
private double secondaryForce;
|
||||
private double mass;
|
||||
private int delay;
|
||||
|
||||
public Haggis(double primaryForce, double secondaryForce, double mass, int delay) {
|
||||
this.primaryForce = primaryForce;
|
||||
this.secondaryForce = secondaryForce;
|
||||
this.mass = mass;
|
||||
this.delay = delay;
|
||||
}
|
||||
|
||||
public double distanceTravelled(int time) {
|
||||
double result;
|
||||
double acc = primaryForce / mass;
|
||||
int primaryTime = Math.min(time, delay);
|
||||
result = 0.5 * acc * primaryTime * primaryTime;
|
||||
|
||||
int secondaryTime = time - delay;
|
||||
if (secondaryTime > 0) {
|
||||
double primaryVelocity = acc * delay;
|
||||
acc = (primaryForce + secondaryForce) / mass;
|
||||
result += primaryVelocity * secondaryTime + 0.5 * acc * secondaryTime + secondaryTime;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable.before;
|
||||
|
||||
public class Order {
|
||||
|
||||
public double discount(double inputValue, int quantity) {
|
||||
if (inputValue > 50) inputValue = inputValue - 2;
|
||||
if (quantity > 100) inputValue = inputValue - 1;
|
||||
return inputValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable.before;
|
||||
|
||||
public class Rectangle {
|
||||
|
||||
private double perimeter;
|
||||
private double area;
|
||||
|
||||
public void updateGeometry(double height, double width) {
|
||||
double temp = 2 * (height + width);
|
||||
System.out.println("Perimeter: " + temp);
|
||||
perimeter = temp;
|
||||
|
||||
temp = height * width;
|
||||
System.out.println("Area: " + temp);
|
||||
area = temp;
|
||||
}
|
||||
|
||||
public double getPerimeter() {
|
||||
return perimeter;
|
||||
}
|
||||
|
||||
public double getArea() {
|
||||
return area;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class HaggisTest {
|
||||
|
||||
@Test
|
||||
void distance() {
|
||||
Haggis haggis = new Haggis(10d, 20d, 10, 5);
|
||||
assertEquals(50d, haggis.distanceTravelled(10));
|
||||
assertEquals(125d, haggis.distanceTravelled(20));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class OrderTest {
|
||||
|
||||
@Test
|
||||
void discount() {
|
||||
Order order = new Order();
|
||||
assertEquals(50d, order.discount(50d, 100));
|
||||
assertEquals(51d - 2, order.discount(51d, 100));
|
||||
assertEquals(50d - 1, order.discount(50d, 101));
|
||||
assertEquals(51d - 2 - 1, order.discount(51d, 101));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._06_mutable_data._18_split_variable;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class RectangleTest {
|
||||
|
||||
@Test
|
||||
void updateGeomerty() {
|
||||
Rectangle rectangle = new Rectangle();
|
||||
rectangle.updateGeometry(10, 5);
|
||||
assertEquals(50d,rectangle.getArea());
|
||||
assertEquals(30d, rectangle.getPerimeter());
|
||||
|
||||
rectangle.updateGeometry(5, 5);
|
||||
assertEquals(25d,rectangle.getArea());
|
||||
assertEquals(20d, rectangle.getPerimeter());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user