From 77f1a97e8fb3cbbbb10d2bf9bcbc12a957495cdc Mon Sep 17 00:00:00 2001 From: pivovarit Date: Mon, 6 Feb 2017 08:07:05 +0100 Subject: [PATCH] Refactor Discounter examples --- .../strategy/ChristmasDiscounter.java | 3 +- .../com/baeldung/strategy/Discounter.java | 16 ++++++++-- .../baeldung/strategy/EasterDiscounter.java | 3 +- .../StrategyDesignPatternUnitTest.java | 31 ++++++++++++++----- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java index 644c57aa26..a0c36bb63e 100644 --- a/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java +++ b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java @@ -3,8 +3,9 @@ package com.baeldung.strategy; import java.math.BigDecimal; public class ChristmasDiscounter implements Discounter { + @Override - public BigDecimal applyDiscount(final BigDecimal amount) { + public BigDecimal apply(BigDecimal amount) { return amount.multiply(BigDecimal.valueOf(0.9)); } } diff --git a/core-java/src/main/java/com/baeldung/strategy/Discounter.java b/core-java/src/main/java/com/baeldung/strategy/Discounter.java index ed6a00cba4..4701287946 100644 --- a/core-java/src/main/java/com/baeldung/strategy/Discounter.java +++ b/core-java/src/main/java/com/baeldung/strategy/Discounter.java @@ -1,7 +1,19 @@ package com.baeldung.strategy; import java.math.BigDecimal; +import java.util.function.UnaryOperator; -public interface Discounter { - BigDecimal applyDiscount(BigDecimal amount); +public interface Discounter extends UnaryOperator { + + 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)); + } } diff --git a/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java index fd70255482..990d10073b 100644 --- a/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java +++ b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java @@ -3,8 +3,9 @@ package com.baeldung.strategy; import java.math.BigDecimal; public class EasterDiscounter implements Discounter { + @Override - public BigDecimal applyDiscount(final BigDecimal amount) { + public BigDecimal apply(BigDecimal amount) { return amount.multiply(BigDecimal.valueOf(0.5)); } } diff --git a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java index b1881091d9..2c1c166b9c 100644 --- a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java +++ b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java @@ -3,10 +3,12 @@ package com.baeldung.strategy; import org.junit.Test; import java.math.BigDecimal; +import java.util.Arrays; 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.util.Lists.newArrayList; public class StrategyDesignPatternUnitTest { @Test @@ -14,7 +16,7 @@ public class StrategyDesignPatternUnitTest { Discounter staffDiscounter = new EasterDiscounter(); final BigDecimal discountedValue = staffDiscounter - .applyDiscount(BigDecimal.valueOf(100)); + .apply(BigDecimal.valueOf(100)); assertThat(discountedValue) .isEqualByComparingTo(BigDecimal.valueOf(50)); @@ -24,13 +26,13 @@ public class StrategyDesignPatternUnitTest { public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() { Discounter staffDiscounter = new Discounter() { @Override - public BigDecimal applyDiscount(final BigDecimal amount) { + public BigDecimal apply( BigDecimal amount) { return amount.multiply(BigDecimal.valueOf(0.5)); } }; final BigDecimal discountedValue = staffDiscounter - .applyDiscount(BigDecimal.valueOf(100)); + .apply(BigDecimal.valueOf(100)); assertThat(discountedValue) .isEqualByComparingTo(BigDecimal.valueOf(50)); @@ -41,18 +43,31 @@ public class StrategyDesignPatternUnitTest { Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5)); final BigDecimal discountedValue = staffDiscounter - .applyDiscount(BigDecimal.valueOf(100)); + .apply(BigDecimal.valueOf(100)); assertThat(discountedValue) .isEqualByComparingTo(BigDecimal.valueOf(50)); } @Test - public void shouldApplyListOfDiscounts() { - List discounters = newArrayList(); + public void shouldApplyAllDiscounts() { + List discounters = Arrays.asList(christmas(), newYear(), easter()); 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 combinedDiscounters = Discounter + .christmas() + .andThen(newYear()); + + combinedDiscounters.apply(BigDecimal.valueOf(100)); } }