P
P
pozitiffcaat2016-05-05 06:58:44
OOP
pozitiffcaat, 2016-05-05 06:58:44

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

4 answer(s)
M
Michael, 2016-05-05
@Sing303

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.

K
kn1ght_t, 2015-06-08
@kn1ght_t

like so

L
LittleFatNinja, 2015-06-08
@LittleFatNinja

<img src="https://habrastorage.org/files/11e/26f/377/11e26f37727f43bc81530ba2235e0d3a.png">
</img>

Z
zuraavl, 2015-06-09
@zuraavl

codepen.io/creme/pen/ntvqe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question