DEV Community

Clean Code Studio
Clean Code Studio

Posted 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)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.