Skip to content

Instantly share code, notes, and snippets.

@ThiagoBarradas
Last active May 6, 2021 06:20
Show Gist options
  • Save ThiagoBarradas/10bf48b7746e681afbf5d35ff94dc204 to your computer and use it in GitHub Desktop.
Save ThiagoBarradas/10bf48b7746e681afbf5d35ff94dc204 to your computer and use it in GitHub Desktop.
SOLID [D] - Dependency inversion
public interface IPaymentMethod
{
bool Pay(int amount);
}
public class CreditCard : IPaymentMethod
{
public bool Pay(int amount)
{
// do something
}
}
public class DebitCard : IPaymentMethod
{
public bool Pay(int amount)
{
// do something
}
}
public class Cash : IPaymentMethod
{
public bool Pay(int amount)
{
// do something
}
}
public interface IPerson
{
IPaymentMethod PaymentMethod { get; }
}
public class Person : IPerson
{
public IPaymentMethod PaymentMethod { get; private set; }
public Person(IPaymentMethod paymentMethod)
{
this.PaymentMethod = paymentMethod;
}
}
// calling
IPaymentMethod creditCard = new CreditCard();
IPerson person = new Person(creditCard);
person.PaymentMethod.Pay(100);
IPaymentMethod cash = new Cash();
IPerson person2 = new Person(cash);
person2.PaymentMethod.Pay(100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment