All I needed were two interfaces!
I recently encountered a perfect example of the Interface Segregation Principle (ISP) during the implementation of a task. Interestingly, I initially failed to recognize it.
The task, in essence, involved populating a collection during the execution of an application, followed by updating this collection with information fetched from a separate thread using an SQS listener. Furthermore, this collection was required to provide certain properties of stored objects to WebSocket message handler.
To achieve this, I encapsulated the collection of objects within a class and restricted its visibility. However, I faced a dilemma: I needed to prevent readers from using the add/remove methods while still allowing this capability for the master object. My solution was to make the add/remove methods package-private, which, in hindsight, was not ideal.
Fortunately, I am currently reading “Clean Architecture,” where the SOLID principles are reiterated. When I came across Part IV about components, it struck me — ISP was the answer. All I needed were two interfaces!
In retrospect, this real-world example serves as an excellent lesson in understanding the Interface Segregation Principle.