SOLID Principles explained in Python with examples.
Go to task_refactor.py
.
Hint: Remove pay method to the class responsible for processing the payment.
Separate the pay methods in different functions. Evaluate the pay_debit
.
Hint:
If we want to add extra payment method we need to modify the payment processor class by adding functions.
Define subclass of PaymentProcessor
for each payment type.
Add PayPal payment type.
PayPal doesnot work with security but with e-mail address.
We are abusing pay type PaymentProcessor method - to do something different than suppose to. Here the argument responsibility. This is violating LS priniciple.
Hint: Add initializer to it to depend on email or security code.
Later, different payment processor sub-class will have extensibility to depend on different attributes via sub-class init. (Pay does not depend on security_code
, but on attribute
initizalized in init
.)
Interfaces Segragation means it is better to have several specific interfaces than one general one purpose interface.
Extend PaymentProcessor
class by sms auth method.
The problem is that the Credit Card
payment method does not include sms auth.
Issue with generic like PaymentProcessor
interfaces is that
they are not always applicable to subclasses.
In this case not all subclasses support 2-FA
It is better to add subclass of PaymentProcessor
that enables 2-FA ( that have more meaningful behaviour).
This is interface segregation.