Skip to content

Design patterns with Spring Boot

Somkiat Puisungnoen edited this page Sep 2, 2026 · 1 revision

Design patterns with Spring Boot

1. Factory Method

  • Payment channels
    • Credit card
    • Debit card
    • Bank transfer
    • Prompt pay

Payment Processor

public interface PaymentProcessor {
    void process(Payment payment);
}

class CreditCardProcessor implements PaymentProcessor {
    @Override
    public void process(Payment payment) {

    }
}

class PromptPaymentProcessor implements PaymentProcessor {
    @Override
    public void process(Payment payment) {

    }
}

Payment Factory

@Component
public class PaymentFactory {

    private Map<String, PaymentProcessor> payments = new HashMap<>();
    public PaymentFactory() {
        payments.put("cc", new CreditCardProcessor());
        payments.put("pp", new PromptPaymentProcessor());
    }

    public PaymentProcessor getPaymentProcessor(String type) {
        return payments.get(type);
    }


}

Payment Service

@Service
public class PaymentService {

    @Autowired
    private PaymentFactory paymentFactory;

    public void process(String type, Payment payment) {
        paymentFactory.getPaymentProcessor(type).process(payment);
    }
}

Clone this wiki locally