I want to create a class that will parse a string in tokens that make sense for my application.
How do I create it?
1 and 2 have the disadvantage that the user can call minor methods without calling the Parse method. I will need to check every small method that was called by the Parse method.
The problem I see in 3 is that the parsing method can potentially do a lot. It just doesn't seem right to put it in ctor.
2 is convenient in that the user can analyze any number of lines without creating an instance of the class again and again.
What is a good approach? What are some of the considerations?
(C # language if someone cares).
thank
I will have a separate class with the Parse method, which takes a string and converts it into a separate new object with a property for each value from the string.
ValueObject values = parsingClass.Parse(theString);
, ...
, - 3 . , , ; - , ? , , , ; ; .
, , , ; . , , , ; Parse(). , - , parse(). , ; , parse() , " " ; , . , , ; - / , , parse() , . , .
, , , , ? , , , , , , , ? , , , , , - , , , , ? , ; , . , ; .
, , , , 10-15 , , ; , , , , . , . , , , , .
, 3. , .
, 2 , , , , . :
// Using option 3 ParsingClass myClass = new ParsingClass(inputString); // Parse a new string. myClass = new ParsingClass(anotherInputString); // Using option 2 ParsingClass myClass = new ParsingClass(); myClass.Parse(inputString); // Parse a new string. myClass.Parse(anotherInputString);
, 2 , , , . ( 1 , , 2 , 3- .)
, Parse , Parse, .
// Option 4 ParsingClass myClass = ParsingClass.Parse(inputString); // Parse a new string. myClass = ParsingClass.Parse(anotherInputString);
1 2 , . 3 4 , . , . , .
:
1) ?
, , . Parse , . , , , show: , .
2) .
, " ", , parse, , , SomethingParser.
, ( factory, ), ParsedSomething.
, , , . , " " - , , , , " ", , , .
, , , ( - ), " ".
In any case, if these things are likely to be parameters, I personally would say that they should be "ready to go" as soon as they are built. If they will only be used locally, you can give users a little more flexibility if they can create them without doing the hard work. Cost requires two lines of code instead of one, which makes your class a little harder to use.
You might consider creating two constructors and the Parse method: the string constructor is equivalent to calling the no-arg constructor, and then calling Parse.