Skip to content

Instantly share code, notes, and snippets.

@rajendhirandev
Created September 11, 2022 11:48
Show Gist options
  • Save rajendhirandev/3f100aa4dfdcd1863cae353b67b97325 to your computer and use it in GitHub Desktop.
Save rajendhirandev/3f100aa4dfdcd1863cae353b67b97325 to your computer and use it in GitHub Desktop.
Delegate Using Kotlin - Code Snippet
package delegation
fun main() {
println(PaymentProcess(CashPayment()).processPayment())
PaymentProcess.isUPIDown = true
println(PaymentProcess(UPIPayment("Tez")).processPayment())
PaymentProcess.isUPIDown = false
println(PaymentProcess(UPIPayment("Paytm")).processPayment())
PaymentProcess.isCardDown = true
println(PaymentProcess(CardPayment("HDFC", "CC")).processPayment())
PaymentProcess.isCardDown = false
println(PaymentProcess(CardPayment("Citi")).processPayment())
PaymentProcess(CashPayment()).paymentMode()
}
interface IPayment {
fun paymentMode(): String
}
class CashPayment : IPayment {
override fun paymentMode(): String {
return "Cash"
}
}
class UPIPayment(val upiName: String) : IPayment {
override fun paymentMode(): String {
return "UPI - $upiName"
}
}
class CardPayment(val providerName: String, val cardType: String = "DC") : IPayment {
override fun paymentMode(): String {
return "Card-$providerName-$cardType"
}
}
const val PROCESSING_ERROR_MSG = "Sorry we can't process it for now with "
const val PROCESSING_MSG = "Process Initiated for "
class PaymentProcess(private val iPayment: IPayment) : IPayment by iPayment {
companion object {
var isCardDown: Boolean = false
var isUPIDown: Boolean = false
}
fun processPayment(): String {
val paymentMode = iPayment.paymentMode()
return if (iPayment !is CashPayment && (isUPIDown || isCardDown)) {
"$PROCESSING_ERROR_MSG $paymentMode"
} else {
"$PROCESSING_MSG $paymentMode"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment