-1

Say i have a class like so:

class Config
{
   private $configA;
   private $configB;
   private $configC;
   private $configD;

   public function getConfigA(): string
   {
        return $this->confiA;
   }
   //...
}

In one sense this class has a single responsibility: managing config settings.

But in another sense it has lots of varying reasons to change: a config is renamed, new config added, config removed, return types changing, validation required on a config etc etc

Should there be a class for each config setting which would satisfy single responsibility or is that too far?

Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
  • Marty what are you doing man! This entry might have some ideas but it is a good study https://en.wikipedia.org/wiki/Design_Patterns. 3/4th's of the way down the patterns are categorized by type. You may be looking for a Structural pattern. – jiveturkey May 23 '17 at 19:24
  • Trying to understand how granular this sort of of thing must be and does my class violate that principle or not – Marty Wallace May 23 '17 at 19:30
  • If you're simply returning strings I'd probably just pack 'em in an array or something but you said they could be different return types which is a little concerning. – jiveturkey May 23 '17 at 19:31

2 Answers2

1

It's not a violation. A simple test is whether or not you have to use the word "and" in a sentence explaining what it does.

Peter Morris
  • 20,174
  • 9
  • 81
  • 146
0

Questions like this are subjective, one needs to look at the context. One such context, a very important one, is the level of abstraction. For example, at an architectural level, single-responsibility is applied to high level components. For instance, in the case of web application, you may think that handling business logic and data access should be separate, handled by separate modules. Although, each of them will have many smaller modules such as classes, interfaces etc. But, at that level of abstraction - architectural level - you may consider each of them as separate single responsibility assigned to modules.

But in a module such as business logic, then level of abstraction is different. You deal with entities at class level. Now, each class has to be based on single responsibility, but at its own level.

One way of achieving this, also class design in general, is to ask what this class is about, a short one word or phrase description. If you can't find such a short description, then it probably needs to be broken down.

Nazar Merza
  • 3,365
  • 1
  • 19
  • 19