Last active
April 27, 2023 21:13
-
-
Save skatesham/7fbb33a36578157c3af23006e9a2bf58 to your computer and use it in GitHub Desktop.
State experimental Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sham.vinicius.fiorin; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.Getter; | |
import java.math.BigDecimal; | |
import java.util.List; | |
/** | |
* Single class State with Visitor pattern | |
*/ | |
public class StatePattern { | |
// State Contract Interface | |
interface StateOperation { | |
void execute(Payment payment); | |
} | |
// Centralize enum | |
@Getter | |
@AllArgsConstructor | |
enum StrategyType { | |
ACTIVE(new ActiveStateOperation()), | |
PENDING(new PendingStateOperation()), | |
INACTIVE(new InactiveStateOperation()); | |
final StateOperation stateOperation; | |
} | |
// Active State Impl | |
static class ActiveStateOperation implements StateOperation { | |
@Override | |
public void execute(Payment payment) { | |
// Subtract from balance | |
payment.setAmount(payment.getAmount().subtract(BigDecimal.valueOf(100))); | |
System.out.println("OPERATION = ACTIVE (minus 100) R:" + payment); | |
} | |
} | |
// Pending State Impl | |
static class PendingStateOperation implements StateOperation { | |
@Override | |
public void execute(Payment payment) { | |
// Same balance remains | |
System.out.println("OPERATION = PENDING (same) R:" + payment); | |
} | |
} | |
// Inactive State Impl | |
static class InactiveStateOperation implements StateOperation { | |
@Override | |
public void execute(Payment payment) { | |
// add value to balance | |
payment.setAmount(payment.getAmount().add(BigDecimal.valueOf(10))); | |
System.out.println("OPERATION = INACTIVE (add 10) R:" + payment); | |
} | |
} | |
// Business Object (BO) | |
@Data | |
@AllArgsConstructor | |
static class Payment { | |
private StrategyType type; | |
private String name; | |
private BigDecimal amount; | |
// Visitor method | |
public void processState() { | |
type.getStateOperation().execute(this); | |
} | |
} | |
// Example execution | |
private static void run() { | |
List<Payment> paymentList = List.of( | |
new Payment(StrategyType.ACTIVE, "Payment 1", BigDecimal.valueOf(1_000)), | |
new Payment(StrategyType.PENDING, "Payment 2", BigDecimal.valueOf(300)), | |
new Payment(StrategyType.INACTIVE, "Payment 3", BigDecimal.valueOf(1_000_000)) | |
); | |
for (Payment payment : paymentList) { | |
payment.processState(); | |
} | |
} | |
public static void main(String[] args) { | |
run(); | |
} | |
/* | |
Print OUTPUT from example: | |
> Task :StatePattern.main() | |
OPERATION = ACTIVE (minus 100) R:StatePattern.Payment(type=ACTIVE, name=Payment 1, amount=900) | |
OPERATION = PENDING (same) R:StatePattern.Payment(type=PENDING, name=Payment 2, amount=300) | |
OPERATION = INACTIVE (add 10) R:StatePattern.Payment(type=INACTIVE, name=Payment 3, amount=1000010) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment