Original Article (in Scala)
These are alternative implementations of the code examples given in the original article, using Java instead of Scala. This only includes the code translation, not the rest of the article. This also only includes the main code examples, as in the implementation of the Strategy Pattern and the FP alternative to it. There are code examples later on in the article that are not translated here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| interface PaymentStrategy {
void execute(double amount);
}
class CreditCardPayment extends PaymentStrategy {
private String cardNumber;
CreditCardPayment(String cardNumber) {
this.cardNumber = cardNumber;
}
void execute(double amount) {
System.out.println("paid " + amount + " using the cc " + cardNumber)
}
}
class PayPalPayment extends PaymentStrategy {
private String email;
PayPalPayment(double email) {
this.email = email;
}
void execute(Double amount) {
System.out.println("paid " + amount + " using paypal user " + email);
}
}
class BitcoinPayment extends PaymentStrategy {
private String walletAddress;
BitcoinPayment(double walletAddress) {
this.waletAddress = walletAddress;
}
void execute(Double amount) {
System.out.println("paid " + amount +
" using the wallet " + walletAddress);
}
}
class PaymentContext {
private PaymentStrategy strategy;
void setStrategy(PaymentStrategy strategy) {
this.strategy = strategy;
}
void executeStrategy(double amount) {
strategy.execute(amount);
}
}
// usage - the `DetailsFromUser` functions are of type `() -> String`
PaymentContext paymentContext = new PaymentContext();
if (someValidatedUserInput.equals("credit card")) {
paymentContext.setStrategy(
new CreditCardPayment(DetailsFromUser.creditCard()));
} else if (someValidatedUserInput.equals("paypal")) {
paymentContext.setStrategy(
new PayPalPayment(DetailsFromUser.paypal()));
} else if (someValidatedUserInput.equals("bitcoin")) {
paymentContext.setStrategy(
new BitcoinPayment(DetailsFromUser.bitcoin());
} else {
throw new RuntimeException("user input invalid");
}
paymentContext.executeStrategy(400);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| class PaymentOptions {
static Function<String, Consumer<Double>> creditCard =
cardNumber -> amount ->
System.out.println("paid " + amount + " using the cc " + cardNumber);
static Function<String, Consumer<Double>> paypal =
email -> amount ->
System.out.println("paid " + amount + " using paypal user " + email);
static Function<String, Consumer<Double>> bitcoin =
walletAddress -> amount ->
System.out.println("paid " + amount +
" using the wallet " + walletAddress);
}
// usage - the `DetailsFromUser` functions are of type `() -> String`
Consumer<Double> payment = null;
if (someValidatedUserInput.equals("credit card"))
payment = PaymentOptions.creditCard.apply(DetailsFromUser.creditCard());
else if (someValidatedUserInput.equals("paypal"))
payment = PaymentOptions.paypal.apply(DetailsFromUser.paypal());
else if (someValidatedUserInput.equals("bitcoin"))
payment = PaymentOptions.bitcoin.apply(DetailsFromUser.bitcoin());
else
throw new RuntimeException("user input invalid");
payment.accept(400);
AtomicReference<Consumer<Double>> paymentRef =
new AtomicReference<>(payment);
|
Note: this code lacks certain capabilities of the Scala, mostly due to the lack of a proper effect system