I am looking for a definitive answer by providing a written example of a problem I so commonly face. Hopefully after months of digging I can try and move forward :-).
I am trying to separate my classes as best as I can for code readability, reusability. I try so dearly to adhere to SOLID principles even when they can bar overkill my practice projects for the sake of learning. I often have a class that has an array which instantiates objects of another class, which in turn will instantiate objects of another class an so on and so on. But I always get confused as to the Single Responsibility Principle with the outermost class. How can this class remain adhering to SRP when it has a multitude of other classes inside it, all with different functionality ?
Let's look at a game example. Each part of the game is separated into separate classes. One aspect of the game means I need a Board Class. This Board class becomes for this purpose by outer most class. It simply holds an array of squares, these squares are instantiated new objects and each square can do different things, these new objects have a property in them which instantiates another object and so on or receives a class as a dependancy via Dependancy injection before instantiation. I am trying to make my classes as small as possible for code readability and to adhere to SRP however I refactor a lot when my dependancies get too large and I start to think it makes more sense to put things in the same class that often refer to each others methods more than a few times and on a regular basis.
But then the outer most class which originally housed just an array of squares which I called the Board class of and who's responsibility it is to find out what square the player landed on (based on the players rolling square count) now houses various other objects and functionality. Not directly as a property but within its single array.
Now the outer Most class ( the Board class, sorry if I am getting repetetive ) has various methods. In each method it firstly calls its original private method which finds the current square we want to target based on the players rolling square count) and then calls a polymorphic method on the targeted square. Polymorphism because each square does different things depending on what square is landed on but can all be targeted using the same method name e.g. activateLogic(). The responsibility of the Board class now becomes not only to find the square the player has landed on but also to call different polymorphic methods on the square depending on what method from the Board class is called from the controller ( which will depend on user input).
So which of these sentences is true
- The Board class does not violate SRP , it has an array and its job is to manage that array , even tho it calls other methods form the square the player has landed on, its responsibility can be defined as "calling polymorphic methods depending on the square landed on."
- The Board class violates SRP as it is calling various different polymorphic methods from various different square objects meaning its doing two much. If this is the case then how would I break up this class adhering to SRP ? Im not sure it's possible as the copy of an array belongs in this class and all the polymorphic methods are contained in this class.
I feel like 1 is correct , but what does this mean for the board class if the square has 20-30 methods which need to be called from the Board class?