How to create a class that has only one hard work method and data returning other methods?

I want to create a class that will parse a string in tokens that make sense for my application.

How do I create it?

  • Provide a ctor that takes a string, provide a Parse method and provide methods (let us call them "minor") that return individual tokens, the number of tokens, etc. OR
  • Provide a ctor that does not accept anything, provide a Parse method that accepts string and secondary methods, as described above. OR
  • Provides a ctor that takes a string and provides only secondary methods, but does not use an analysis method. Parsing is performed by ctor.

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

+3
source share
4 answers

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);
+4

, ...

, - 3 . , , ; - , ? , , , ; ; .

, , , ; . , , , ; Parse(). , - , parse(). , ; , parse() , " " ; , . , , ; - / , , parse() , . , .

, , , , ? , , , , , , , ? , , , , , - , , , , ? , ; , . , ; .

, , , , 10-15 , , ; , , , , . , . , , , , .

+1

, 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 , . , . , .

0

:

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.

0
source

All Articles