2

What's the name for a class that has only methods? There are no fields/properties. Just two methods with the ability to parse some file in two ways. I have named the class Parser, but it just doesn't seem right to have a class do only that. Or?

Should it be in a class ?

EDIT:

Dummy Example

class Parser
{
     public int parseMethod1(string file)
     {
         //parse & return
     }

     public string[] parseMethod2(string file)
     {
         //parse & return
     }
}

How could I write an interface IParser which allowed me to have a subclass only implement one of the methods?

  • 3
    and let me guess, those methods are static. Not every problem is a good fit for OOP, and sometimes a procedural solution is preferable. You could refer to a collection of procedures as a module. However, I think that in your case an OOP solution would be preferable – why didn't you write two subclasses that each implement a way of parsing the file, and have Parser be an interface? Without more context, all we can do is to take a wild guess. – amon Feb 3 '14 at 22:31
  • 1
    Should it be in a class ? - it is in a class, isn't it? And where else could it be (in C#) – Konrad Morawski Feb 3 '14 at 22:34
  • Why would you want to call it a special name? – oɔɯǝɹ Feb 3 '14 at 22:40
  • @amon The two methods I have that parse files, have different return types. How would you do this with an interface? I mean don't you HAVE to implement all methods ? So if I wrote a subclass I would have to implement both parse methods even though I only needed one. Or am I completely confused here? – DSF Feb 3 '14 at 22:51
  • 1
    @D.Singh How about generics (aka. type parameters)? interface Parser<T> { public T parse(string); } class SomeParser implements Parser<int> { public int parse(string file) { ... } } (sorry, I'm used to Java syntax) – amon Feb 3 '14 at 22:55
6

An object-oriented design may actually be appropriate here, but both your parse methods belong in separate subclasses. You can use generics (type parameters) to achieve this. Something like:

interface IParser<T>
{
    public T parse(string);
}

class SomeParser : IParser<int>
{
    public int parse(string file)
    {
        ...
    }
}

class AnotherParser : IParser<string[]>
{
    public string[] parse(string file)
    {
        ...
    }
}

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.