Skip to content

Instantly share code, notes, and snippets.

@skatesham
Last active April 6, 2023 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skatesham/7ae4c5fb7ecc85d93ce9ff9d94b33062 to your computer and use it in GitHub Desktop.
Save skatesham/7ae4c5fb7ecc85d93ce9ff9d94b33062 to your computer and use it in GitHub Desktop.
Pattern Strategy With Visitor Embeded in Enum on Domain Object
package core.chapadaguides;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.ToString;
import java.math.BigDecimal;
import java.util.List;
public class StrategyMain {
interface Strategy {
Result execute(Payment payment);
default Result calculateResult(Payment payment, double feeAmountPercentage1) {
BigDecimal amount = payment.getAmount();
BigDecimal amountFee = amount.multiply(BigDecimal.valueOf(feeAmountPercentage1));
BigDecimal netAmount = amount.subtract(amountFee);
return Result.of(amount, netAmount, amountFee);
}
}
@Getter
@AllArgsConstructor
enum StrategyType {
CREDIT_CARD(new CreditCardStrategy()),
DEBIT_CARD(new DebitCardStrategy()),
PIX(new PixStrategy());
final Strategy strategy;
}
static class CreditCardStrategy implements Strategy {
final double feeAmountPercentage = 0.10; // 10%
@Override
public Result execute(Payment payment) {
return calculateResult(payment, feeAmountPercentage);
}
}
static class DebitCardStrategy implements Strategy {
final double feeAmountPercentage = 0.05; // 5%
@Override
public Result execute(Payment payment) {
return calculateResult(payment, feeAmountPercentage);
}
}
static class PixStrategy implements Strategy {
final double feeAmountPercentage = 0.01; // 1%
@Override
public Result execute(Payment payment) {
return calculateResult(payment, feeAmountPercentage);
}
}
@Data
@AllArgsConstructor
static class Payment {
private StrategyType type;
private String name;
private BigDecimal amount;
public Result calculateAmount() {
return type.getStrategy().execute(this);
}
}
@ToString
@Data
@AllArgsConstructor(staticName = "of")
static class Result {
final BigDecimal amount;
final BigDecimal netAmount;
final BigDecimal amountFee;
}
private static void process() {
List<Payment> paymentList = List.of(
new Payment(StrategyType.CREDIT_CARD, "Boll", BigDecimal.valueOf(1_000)),
new Payment(StrategyType.DEBIT_CARD, "Phone", BigDecimal.valueOf(300)),
new Payment(StrategyType.PIX, "Car", BigDecimal.valueOf(1_000_000))
);
for (Payment payment : paymentList) {
Result result = payment.calculateAmount();
System.out.println(payment.getName() + " | " + payment.getType() + " : " + result);
}
// ------> Output <-------
// ------------------------------------------------------------------------------------
// Boll | CREDIT_CARD : StrategyMain.Result(amount=1000, netAmount=900.0, amountFee=100.0)
// Phone | DEBIT_CARD : StrategyMain.Result(amount=300, netAmount=285.00, amountFee=15.00)
// Car | PIX : StrategyMain.Result(amount=1000000, netAmount=990000.00, amountFee=10000.00)
}
public static void main(String[] args) {
process();
}
}
@skatesham
Copy link
Author

The idea was simplify the Strategy and so the mutability became from the Domain Object (DO)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment