Answer the question
In order to leave comments, you need to log in
How to properly implement a parser following the Single responsibility principle?
There is a parser that parses, for example, kittens, dogs, cars, buildings and aliens.
This class is engaged in the fact that it receives a bunch of bytes as input, and a data structure as output.
Do I understand correctly that this class has 5 reasons to change? After all, at different times of life, the format of these same kittens and dogs with aliens can change. Or maybe it doesn’t come in bytes at all, but in Json or Xml strings (But here it’s obvious that strings can simply be translated into an array of bytes).
How to correctly interpret this principle?
Answer the question
In order to leave comments, you need to log in
Well, that's right. If you have one class trying to parse both HTML and JSON, then obviously it needs to be divided into at least 2 classes. You can, for example, highlight the general method Parse. Create a certain IParser interface and implement it in these 2 classes.
<img src="https://habrastorage.org/files/11e/26f/377/11e26f37727f43bc81530ba2235e0d3a.png">
</img>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question