Kites Kites - 1 year ago 77
Python Question

When following SOLID, are reading and writing files two separate responsibilities?

The question may seem silly but I'm just starting to explore SOLID and I'm unsure if reading from files and writing to files are the same responsibility. The target is the same file type; I want to read and write .pdf's in my application. The application is in Python if that makes any difference.

Answer Source

The reading and writing implementation have a high probability of being highly cohesive. If one would change, so would the other. High cohesion is a strong indication of a Single Responsibility and the Single Responsibility Principle tells us that they should be put together in the same class.

If however there are consumers that only read data without writing, or only write without reading, it is a indication that from an interface perspective you should separate these operations, as prescribed by the Interface Segregation Principle. This means that the consumers should define two interfaces that they can depend on, while the File class will implement both interfaces.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download