DEV Community

Clean Code Studio
Clean Code Studio

Posted on • Edited on

Python Single Responsibility Design Pattern (Code Example)

Python Single Responsibility Design Pattern

_"Every module, class, or function should have only one reason to change."_

Example of the Single Responsibility Principle design pattern implemented in Python code:

In this example, the Journal class has a single responsibility, which is to store and manage journal entries.

The PersistenceManager class, on the other hand, has a single responsibility, which is to persist the Journal to different storage mediums (file, web, etc.).

This separation of responsibilities makes the code more maintainable and easier to understand, as changes to one class won't affect the other.

Python
Design Patterns
Clean Code Studio
Python Design Patterns
Single Responsibility Principle (SRP)

Top comments (0)