Skip to content

Instantly share code, notes, and snippets.

@shan-shaji
Last active July 31, 2023 19:19
Show Gist options
  • Save shan-shaji/1246191c09fe4ebde4b30ce750bf521f to your computer and use it in GitHub Desktop.
Save shan-shaji/1246191c09fe4ebde4b30ce750bf521f to your computer and use it in GitHub Desktop.
Dart-Liskov-substitution

Dart-Liskov-substitution

Created with <3 with dartpad.dev.

// 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