Refactor Discounter examples
This commit is contained in:
parent
daf7af813c
commit
77f1a97e8f
@ -3,8 +3,9 @@ package com.baeldung.strategy;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public class ChristmasDiscounter implements Discounter {
|
public class ChristmasDiscounter implements Discounter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BigDecimal applyDiscount(final BigDecimal amount) {
|
public BigDecimal apply(BigDecimal amount) {
|
||||||
return amount.multiply(BigDecimal.valueOf(0.9));
|
return amount.multiply(BigDecimal.valueOf(0.9));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
package com.baeldung.strategy;
|
package com.baeldung.strategy;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
public interface Discounter {
|
public interface Discounter extends UnaryOperator<BigDecimal> {
|
||||||
BigDecimal applyDiscount(BigDecimal amount);
|
|
||||||
|
static Discounter christmas() {
|
||||||
|
return (amount) -> amount.multiply(BigDecimal.valueOf(0.9));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Discounter newYear() {
|
||||||
|
return (amount) -> amount.multiply(BigDecimal.valueOf(0.8));
|
||||||
|
}
|
||||||
|
|
||||||
|
static Discounter easter() {
|
||||||
|
return (amount) -> amount.multiply(BigDecimal.valueOf(0.5));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,9 @@ package com.baeldung.strategy;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
public class EasterDiscounter implements Discounter {
|
public class EasterDiscounter implements Discounter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BigDecimal applyDiscount(final BigDecimal amount) {
|
public BigDecimal apply(BigDecimal amount) {
|
||||||
return amount.multiply(BigDecimal.valueOf(0.5));
|
return amount.multiply(BigDecimal.valueOf(0.5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,12 @@ package com.baeldung.strategy;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static com.baeldung.strategy.Discounter.*;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.util.Lists.newArrayList;
|
|
||||||
|
|
||||||
public class StrategyDesignPatternUnitTest {
|
public class StrategyDesignPatternUnitTest {
|
||||||
@Test
|
@Test
|
||||||
@ -14,7 +16,7 @@ public class StrategyDesignPatternUnitTest {
|
|||||||
Discounter staffDiscounter = new EasterDiscounter();
|
Discounter staffDiscounter = new EasterDiscounter();
|
||||||
|
|
||||||
final BigDecimal discountedValue = staffDiscounter
|
final BigDecimal discountedValue = staffDiscounter
|
||||||
.applyDiscount(BigDecimal.valueOf(100));
|
.apply(BigDecimal.valueOf(100));
|
||||||
|
|
||||||
assertThat(discountedValue)
|
assertThat(discountedValue)
|
||||||
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
||||||
@ -24,13 +26,13 @@ public class StrategyDesignPatternUnitTest {
|
|||||||
public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
|
public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() {
|
||||||
Discounter staffDiscounter = new Discounter() {
|
Discounter staffDiscounter = new Discounter() {
|
||||||
@Override
|
@Override
|
||||||
public BigDecimal applyDiscount(final BigDecimal amount) {
|
public BigDecimal apply( BigDecimal amount) {
|
||||||
return amount.multiply(BigDecimal.valueOf(0.5));
|
return amount.multiply(BigDecimal.valueOf(0.5));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final BigDecimal discountedValue = staffDiscounter
|
final BigDecimal discountedValue = staffDiscounter
|
||||||
.applyDiscount(BigDecimal.valueOf(100));
|
.apply(BigDecimal.valueOf(100));
|
||||||
|
|
||||||
assertThat(discountedValue)
|
assertThat(discountedValue)
|
||||||
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
||||||
@ -41,18 +43,31 @@ public class StrategyDesignPatternUnitTest {
|
|||||||
Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5));
|
Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5));
|
||||||
|
|
||||||
final BigDecimal discountedValue = staffDiscounter
|
final BigDecimal discountedValue = staffDiscounter
|
||||||
.applyDiscount(BigDecimal.valueOf(100));
|
.apply(BigDecimal.valueOf(100));
|
||||||
|
|
||||||
assertThat(discountedValue)
|
assertThat(discountedValue)
|
||||||
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
.isEqualByComparingTo(BigDecimal.valueOf(50));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldApplyListOfDiscounts() {
|
public void shouldApplyAllDiscounts() {
|
||||||
List<Discounter> discounters = newArrayList();
|
List<Discounter> discounters = Arrays.asList(christmas(), newYear(), easter());
|
||||||
|
|
||||||
BigDecimal amount = BigDecimal.valueOf(100);
|
BigDecimal amount = BigDecimal.valueOf(100);
|
||||||
|
|
||||||
discounters.forEach((d) -> d.applyDiscount(amount));
|
final Discounter combinedDiscounter = discounters
|
||||||
|
.stream()
|
||||||
|
.reduce(v -> v, (d1, d2) -> (Discounter) d1.andThen(d2));
|
||||||
|
|
||||||
|
combinedDiscounter.apply(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldChainDiscounters() {
|
||||||
|
final Function<BigDecimal, BigDecimal> combinedDiscounters = Discounter
|
||||||
|
.christmas()
|
||||||
|
.andThen(newYear());
|
||||||
|
|
||||||
|
combinedDiscounters.apply(BigDecimal.valueOf(100));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user