Created
October 3, 2018 12:09
-
-
Save jsstrn/d4aae73630fcf62c226451591abad013 to your computer and use it in GitHub Desktop.
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
class Payment { | |
constructor() { | |
this.paymentMethod = '' | |
} | |
setPaymentMethod(paymentMethod) { | |
this.paymentMethod = paymentMethod | |
} | |
pay(amount) { | |
this.paymentMethod.pay(amount) | |
} | |
} | |
class CashPayment { | |
constructor() {} | |
pay(amount) { | |
// make payment | |
} | |
} | |
class CreditPayment { | |
constructor() {} | |
pay(amount) { | |
// make payment | |
} | |
} | |
const payment = new Payment() | |
const cash = new CashPayment() | |
const credit = new CreditPayment() | |
payment.setPaymentMethod(credit) | |
payment.pay() // pay with credit | |
payment.setPaymentMethod(cash) | |
payment.pay() // pay with cash |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Personally, Payment would rather be an interface than an actual class because Payment doesn't hold any logic. I imagine there is a scenario that an user is able to set his payment option as Credit