Usage is simple in both cases, but what does it mean to enable parameter input to Parser1, compared to the other?
Its a fundamental design shift. And design should convey intent and meaning. Do you need to have separate objects for every string you want to parse? In other words, why do we need an instance of parser with stringX and another instance with stringY? What is it about parse(ing) and the given string that the two must live and die together? Assuming that the "underlying [parsing] implementation" (as Robert Harvey says) does not change, there seems to be no point. And even then its questionable IMHO.
How does the concept of a class change when passing data to the constructor instead of method parameters?
Constructor parameters tell me these things are required for an object. Proper state is not guaranteed without them. Also, I know how/why one parser is fundamentally different from another.
Constructor parameters keep me from having to know too much about how to use the class. If instead I'm supposed to set certain properties - how do I know that? A whole can of worms opens up. What properties? In what order? Before I use what methods? and so on.
Another question comes up when I realize that an interface would be quite meaningless in the second implementation:
An interface, as in A.P.I., is the methods and properties exposed to client code. Do not get wrapped up in public interface { ... }
exclusively. So the meaning of the interface is in the either-or constructor vs method parameter dilemma, NOT public interface Iparser
vs public sealed class Parser
The sealed
class is odd. If I'm thinking about different parser implementations - you did mention "Iparser" - then inheritance is my first thought. It's just a natural conceptual extension in my thinking. I.E. all ParserX
s are fundamentally Parser
s. How else to say it?... A German Shepard is a dog (inheritance), but I can train my parrot to bark (act like a dog - "interface"); but Polly is not a dog, merely pretending, having learned a subset of dogness. Classes, abstract or otherwise, serve perfectly well as interfaces.