Created with <3 with dartpad.dev.
Last active
July 31, 2023 19:19
-
-
Save shan-shaji/1246191c09fe4ebde4b30ce750bf521f to your computer and use it in GitHub Desktop.
Dart-Liskov-substitution
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
// Liskov substitution principle | |
void main() { | |
PayPalProcessor payPalProcessor = PayPalProcessor(); | |
VisaPaymentProcessor visaProcessor = VisaPaymentProcessor(); | |
// Here the checkout function don't now which service it is using to process the payment. | |
// THe underlying implementation is hidden from the checkout function. | |
// | |
// I can add more function to the payment processor and can extend it's functionality. | |
checkout(payPalProcessor, 230); | |
checkout(visaProcessor, 250); | |
} | |
void checkout(PaymentProcessor processor, double amount) { | |
processor.processPayment(amount); | |
} | |
class PaymentProcessor { | |
void processPayment(amount) { | |
print('PaymentProcessor - completed'); | |
} | |
} | |
class PayPalProcessor extends PaymentProcessor { | |
@override | |
void processPayment(amount) { | |
print('PayPalProcessor - completed'); | |
} | |
} | |
class VisaPaymentProcessor extends PaymentProcessor { | |
@override | |
void processPayment(amount) { | |
print('VisaPaymentProcessor - completed'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment