1

I have a legacy class XMLReaderWriter which I think I should break into two classes so that it doesn't break the SRP. Unfortunately the class is already in production and I have to maintain backwards compatibility. What would be the best way to go about this? Any relevant design patterns that I should use?

  • 1
    It not violate SOLID, and SRP particularly. Because if you make changes in a Writer part you must make changes in a Reader part as well. So keeping both in one class is totally SRP compliant. If I am wrong show the code then ;) – Fabio Dec 23 '18 at 17:37
  • 5
    Whens the last time you had to make changes to the XML class? How many of those times was it a major hassle? Is changing this class going to solve a practical problem? – whatsisname Dec 23 '18 at 17:52
  • If you split this class in two, and then change logic in the Writer, do you need to do correspondent change in the Reader as well? – Fabio Dec 24 '18 at 22:16
9

I'd like to know what problems this class is actually causing you. The "Single Responsibility Principle" is one of the most misunderstood, and lots of time has been wasted by placating it.

In other words, "violating the single responsibility principle" on its own is not a reason to spend any time. Wait until it causes you actual problems.

8

Without code we can't validate the violation, or give specific answers, but going on the assumption that your needs are correct, I would suggest creating your SRP-compliant XMLReader and XMLWriter classes, and updating the current XMLReaderWriter to simply be a facade, outsourcing all of it's logic to your 2 new classes.

  • 2
    IF OP decides to split up into two classes, this is an excellent plan. – user949300 Dec 23 '18 at 18:10
  • 1
    What benefit you will gain by having three classes instead of one? – Fabio Dec 24 '18 at 1:12
  • 2
    @Fabio Maybe you meant to ask OP this question? The answer presumes that the class is to be split. That's already two classes. If you maintain the original for backwards compatibility, that's three. – Neil Dec 24 '18 at 7:31
  • @Neil, answer clearly stays for having two classes for reader and writer and third (original) as facade. My point was that if OP already have "facade" which uses XmlReader and XmlWriter inside, then introducing two other wrappers will be overkill. But again without context it just speculation. I wonder that question and answers got so much attention, without context. – Fabio Dec 24 '18 at 8:24
  • @Fabio Unfortunately the class is already in production this statement makes us think that OP is concerned about backward compatibility. – Laiv Dec 24 '18 at 18:03

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.