Last active
May 6, 2021 06:20
-
-
Save ThiagoBarradas/10bf48b7746e681afbf5d35ff94dc204 to your computer and use it in GitHub Desktop.
SOLID [D] - Dependency inversion
This file contains hidden or 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
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