fer-git / Solid_Principles_Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Requirements

Install and use Python >= 3.7 as I am using dataclasses.

The five SOLID principles are:

  • Single responsibility principle: a class should have one, and only one, reason to change.

  • Open-closed principle: software entities should be open for extension, but closed for modification. It should be possible to extend the behavior of a class without modifying it.

  • Liskov Substitution principle: subclasses should be substitutable for their superclasses.

  • Interface segregation principle: many small, client-specific interfaces are better than big general interfaces.

  • Dependency inversion principle: software entities should depend on abstractions, not in concretions.

Notes

You will find a folder inside "src" for every SOLID principle. And for each of them:

  • "wrong" folder contains code that violates the principle.
  • "right" folder contains code that satisfies the principle after some modifications of the code inside "wrong".

There is on exception. I am applying the Interface segregation Principle in order to satisfy the Liskov Substitution Principle. That's why:

  • The folder of the Liskov Substitution Principle ("LIS") just contains a subfolder named "wrong".
  • The folder of the Interface Segregation Principle ("ISP") just contains a subfolder named "right".

There is a folder called "diagrams" with the diagrams of these code examples, both in .png and .drawio formats.

To delete pycache folders:

find . -type d -name "__pycache__" -exec rm -r {} \;

I am using main.py and packages in this project.

Some resources:

To execute the examples

You should be in the root folder "Solid_Principles_Python"

To execute SRP examples:

python3 -m src.SRP.wrong
python3 -m src.SRP.right

To execute OCP examples:

python3 -m src.OCP.wrong
python3 -m src.OCP.right

To execute LIS example:

python3 -m src.LIS.wrong

To execute ISP example:

python3 -m src.ISP.right

To execute DIP examples:

python3 -m src.DIP.wrong
python3 -m src.DIP.right

About


Languages

Language:Python 100.0%