Skip to content

Instantly share code, notes, and snippets.

@ThiagoBarradas
Created January 10, 2021 00:45
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 ThiagoBarradas/4c7b5c036843d9e82fa615863c12ad16 to your computer and use it in GitHub Desktop.
Save ThiagoBarradas/4c7b5c036843d9e82fa615863c12ad16 to your computer and use it in GitHub Desktop.
SOLID [O] - Deciding implementations
// sample enum for payment.Type
public enum PaymentType
{
CreditCard,
DebitCard,
BankInvoice,
Pix
}
// simple factory
public static class PaymentServiceFactory
{
public IPaymentService GetProcessor(PaymentType type)
{
switch (type)
{
case PaymentType.CreditCard:
return new CreditCardPaymentService();
case PaymentType.DebitCard:
return new DebitCardPaymentService();
case PaymentType.BankInvoice:
return new BankInvoicePaymentService();
case PaymentType.Pix:
return new PixPaymentService();
default:
throw new NotImplementedException("Payment type not implemented");
}
}
}
// example calling by action into controller
[HttpPost("payments")]
public IActionResult ProcessPayment([FromBody] CreatePaymentRequest request)
{
// do something
var payment = request.Map<Payment>();
var paymentProcessor = PaymentServiceFactory.GetProcessor(payment.Type);
var result = paymentProcessor.ProcessPayment(payment);
// return something
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment