Skip to content

Commit cc60a88

Browse files
committed
Add standard strategy design pattern with test case
1 parent d985b64 commit cc60a88

File tree

9 files changed

+145
-9
lines changed

9 files changed

+145
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
import java.time.LocalDate;
4+
5+
record CreditCardStrategy(String name, String cardNumber, String cvv, LocalDate dateOfExpiry) implements PaymentStrategy {
6+
7+
@Override
8+
public void pay(double amount) {
9+
System.out.println(amount + " paid by 💳 credit card!");
10+
}
11+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
record Item(String upcCode, String name, int priceInCents) {
4+
5+
public double price() {
6+
return (double) priceInCents / 100;
7+
}
8+
9+
@Override
10+
public boolean equals(Object o) {
11+
if (this == o) return true;
12+
if (!(o instanceof Item item)) return false;
13+
14+
return upcCode.equals(item.upcCode);
15+
}
16+
17+
@Override
18+
public int hashCode() {
19+
return upcCode.hashCode();
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
public interface PaymentStrategy {
4+
5+
void pay(double amount);
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
public record PaypalStrategy(String emailId, String password) implements PaymentStrategy {
4+
5+
@Override
6+
public void pay(double amount) {
7+
System.out.println(amount + " paid with 📱 paypal!");
8+
}
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
class Shopping {
4+
5+
private PaymentStrategy strategy;
6+
7+
Shopping(PaymentStrategy strategy) {
8+
this.strategy = strategy;
9+
}
10+
11+
void setStrategy(PaymentStrategy strategy) {
12+
this.strategy = strategy;
13+
}
14+
15+
void pay(ShoppingCart cart) {
16+
pay(cart, strategy);
17+
}
18+
19+
void pay(ShoppingCart cart, PaymentStrategy strategy) {
20+
double total = cart.computeTotal();
21+
strategy.pay(total);
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
class ShoppingCart {
7+
8+
private final Map<Item, Integer> itemQuantities = new LinkedHashMap<>();
9+
10+
void addItem(Item item) {
11+
addItem(item, 1);
12+
}
13+
14+
void addItem(Item item, int quantities) {
15+
assert quantities >= 1;
16+
itemQuantities.merge(item, quantities, Integer::sum);
17+
}
18+
19+
double computeTotal() {
20+
return itemQuantities.entrySet()
21+
.stream()
22+
.mapToDouble(entry -> entry.getKey().price() * entry.getValue())
23+
.sum();
24+
}
25+
}

DesignPatterns/src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy/MathUtils.java renamed to DesignPatterns/src/main/java/pl/mperor/lab/java/design/pattern/behavioral/strategy/lightweight/MathUtils.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
package pl.mperor.lab.java.design.pattern.behavioral.strategy;
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.lightweight;
22

33
import java.util.List;
44
import java.util.function.Predicate;
55

6-
public class MathUtils {
6+
class MathUtils {
77

8-
public static int totalValue(List<Integer> values) {
8+
static int totalValue(List<Integer> values) {
99
return totalValue(values, _ -> true);
1010
}
1111

12-
public static int totalValue(List<Integer> values, Predicate<Integer> selector) {
12+
static int totalValue(List<Integer> values, Predicate<Integer> selector) {
1313
return values.stream()
1414
.filter(selector)
1515
.mapToInt(e -> e)
1616
.sum();
1717
}
1818

19-
public static boolean isEven(int number) {
19+
static boolean isEven(int number) {
2020
return number % 2 == 0;
2121
}
2222

23-
public static boolean isOdd(int number) {
23+
static boolean isOdd(int number) {
2424
return !isEven(number);
2525
}
2626
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.classic;
2+
3+
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import pl.mperor.lab.common.TestUtils;
7+
8+
import java.time.LocalDate;
9+
10+
public class PaymentStrategyTest {
11+
12+
@Test
13+
public void shouldShoppingAllowToPayWithDifferentPaymentStrategy() {
14+
Shopping shopping = new Shopping(new PaypalStrategy("[email protected]", "1234"));
15+
ShoppingCart shoppingCart = new ShoppingCart();
16+
shoppingCart.addItem(new Item("b1", "Smart TV", 999_00));
17+
shoppingCart.addItem(new Item("b2", "Raspberry Pi", 99_99));
18+
shoppingCart.addItem(new Item("b3", "DVD Player", 44_95));
19+
20+
var out = TestUtils.setTempSystemOut();
21+
shopping.pay(shoppingCart);
22+
shopping.setStrategy(new CreditCardStrategy("John Doe", "1234 1111 2222 1010", "123", LocalDate.parse("2025-12-31")));
23+
shopping.pay(shoppingCart);
24+
shopping.pay(shoppingCart, _ -> System.out.println("Fake payment 💰, do not use in production!"));
25+
26+
var outLines = out.lines();
27+
Assertions.assertTrue(outLines.getFirst().endsWith("paid with 📱 paypal!"));
28+
Assertions.assertTrue(outLines.getSecond().endsWith("paid by 💳 credit card!"));
29+
Assertions.assertEquals("Fake payment 💰, do not use in production!", outLines.getThird());
30+
TestUtils.resetSystemOut();
31+
}
32+
33+
@Test
34+
public void shouldReturnCorrectTotalPriceWhenItemsAdded() {
35+
var cart = new ShoppingCart();
36+
cart.addItem(new Item("A1", "iPhone X", 2000 * 100));
37+
cart.addItem(new Item("A2", "Laptop", 2500 * 100), 2);
38+
cart.addItem(new Item("A3", "Tablet Neo", 199 * 100), 3);
39+
Assertions.assertEquals(7597d, cart.computeTotal());
40+
}
41+
}

DesignPatterns/src/test/java/pl/mperor/lab/java/design/pattern/behavioral/strategy/LightweightStrategyTest.java renamed to DesignPatterns/src/test/java/pl/mperor/lab/java/design/pattern/behavioral/strategy/lightweight/SelectorStrategyTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package pl.mperor.lab.java.design.pattern.behavioral.strategy;
1+
package pl.mperor.lab.java.design.pattern.behavioral.strategy.lightweight;
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

66
import java.util.List;
77

8-
public class LightweightStrategyTest {
8+
public class SelectorStrategyTest {
99

1010
@Test
11-
public void testTotalValueStrategy() {
11+
public void testMathUtilsWithSelectorStrategy() {
1212
var values = List.of(1, 2, 3, 4);
1313
Assertions.assertEquals(10, MathUtils.totalValue(values));
1414
Assertions.assertEquals(6, MathUtils.totalValue(values, MathUtils::isEven));

0 commit comments

Comments
 (0)