Skip to content

SOLID Principles explained in Python with examples.

License

Notifications You must be signed in to change notification settings

kkazimierska/solid.python

 
 

Repository files navigation

SOLID Principles explained in Python with examples.

Task 1. Refactor class to comply with S(OLID) principle.

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.

Task 2. Refactor class to comply with (S)O(LID) principle.

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.

Task 3. Refactor class to comply with SO(L)ID principle.

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.

Task 4. Refactor class to comply with SOL(I)D principle.

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.

About

SOLID Principles explained in Python with examples.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%