Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much info should I put into a class? (OOP)

Tags:

c#

oop

class

I'm a 1st level C# programming student, though I've been dabbling in programming for a few years, and learning above and beyond what the class teaches me is just what I'm doing so that I'm thoroughly prepared once I get out into the job environment. This particular class isn't OOP at all, that's actually the next class, but for this project the teacher said he wouldn't mind if we went above and beyond and did the project in OOP (in fact you can't get an A in his class unless you go above and beyond anyways).

The project is(at this point) to read in an XML file, byte by byte, store element tags to one array, and the data values to another. I fought with him on this(given the .net frameworks dealing on XML) but that was a losing battle. He wants us to code this without using .net XML stuff.

He did provide an example of OOP for this program that he slopped together (originally written in Java, ported to C++, then ported from C++ to C#)

In his example he's got three classes. the first, XMLObject, which contains the arrays, a quasi constructor, getter and setter methods(not properties, which I plan to fix in my version), and a method for adding the < and > to tags to be stored in the arrays (and output to console if need be.)

The second class is a parseXML class. In this one he has fields that keep track of the line count, file offset, tag offset, and strings to hold elements and data. Again, he's got getter and setter methods, several parse methods that search for different things, and a general parse method that uses the other parse methods(sort of combines them here). Some of these methods make calls to the XMLObject class's methods, and send the parsed element and data values to their respective arrays.

The third class he has is one that has no fields, and has two methods, one for doing ATOI and one for dumping a portion of the file stream to the console.

I know we're essentially building a less efficient version of what's already included in the .net framework. I've pointed this out to him and was told "do not use .net's XML class, end of discussion" so let's all agree to just leave that one alone.

My question is, should those really be 3 separate classes. Shouldn't the parsing class either inherit from the XML object class, or just be coded in the XML object class, and shouldn't the ATOI and dumping methods be in one of those two classes as well?

It makes sense to me that if the parsing class's aim in life is to parse an XML file and store elements and data fields to an array, it should be in the same class rather than being isolated and having to do it through getters and setters(or properties in the version I'm going to do). I don't see why the arrays would need to be encapsulated away from the parse methods that actually give them what to store.

Any help would be appreciated, as I'm still designing this, and want to do it at least as close to "proper"(I know it's a relative term) OOP form.

like image 983
Tyler W Avatar asked May 23 '11 13:05

Tyler W


4 Answers

The general rule is that we count the size of a class in the number of responsibilities that it has:

A class should have a single responsibility: a single reason to change.

It seems to me that your teacher did separate his responsibilities correctly. He separated the presentation from the xml parsing logic, and he separated the xml data from the xml parsing behavior.

like image 141
Steven Avatar answered Sep 25 '22 06:09

Steven


First: If you're in a programming class, there may be a good reason he wants you to do this by hand: I really don't recommend arguing with your professors. You'll never win, and you can hurt your grades.

Second: His version is not (considering the fact that it is largely a re-writing of parts of the System.XML namespace) too terrible. Basically you have one class that "Is" your XML. Think of it like the XDocument or XmlDocument classes: Basically it just contains the Xml itself. Then You have your Xml Parser: think of that like XmlReader. And your last one is sort of his equivalent of XmlWriter.

Remember that with OOP, your Xml class (the one that represents the document itself) should neither know nor care how it came into possession of the information it has. Further, the Parser should know how to get the Xml, but it shouldn't much care where it gets stored. Finally, your Writer class shouldn't really care where the data is coming from, only where it's going.

I know it's over-used, but think of your program like a car- it has several parts that all have to work together, but you should be able to change any given part of it without majorly affecting the other pieces. If you lump everything in one class, you lose that flexibility.

like image 24
AllenG Avatar answered Sep 25 '22 06:09

AllenG


Some points:

  • Classes are nouns; methods are verbs.
    Your class should be called XmlParser.

  • Since the XML parser is neither part of the XMLObject nor extends the XMLObject, it should be a separate class.

  • The third class has nothing to do with either of the other two; it's just an ordinary Utilities class.

  • In general, each class should be responsible for a single unit of work or storage.
    Don't try to put too much into a single class (see the "God object" anti-pattern).
    There's nothing wrong with having lots of classes. (As long as they all make sense)

like image 42
SLaks Avatar answered Sep 26 '22 06:09

SLaks


Let's summarize what the system must do :

  • to read in an xml file, byte by byte,
  • store element tags to one array,
  • the data values to another.

I would probably slice it up in the following way:

  • Reader : Given a file path, yields the contents byte-wise (IEnumerable<byte>)
  • Tokenizer: Given an enumeration of bytes, yields tokens relevant to the XML-Context (IEnumerable<XmlToken>)
  • XmlToken : Base class to any output that the tokenizer produces. For now you need 2 specializations :
    • Tag : An opening tag
    • Value : Contents of a tag
  • TokenDelegator : Accepts a Tokenizer and an instance of
  • IXmlTokenVisitor: (See Visitor pattern)
  • TagAndValueStore: Implements IXmlTokenVisitor. Visit(Tag tag) and Visit(Value value) are implented and the relevant content stored in arrays.

You see, I ended up with 7 classes and 1 interface. But you may notice that you have laid the foundations for a fully-fledged XML parser.

Often code that is sold to be OO just plain isn't. A class should adhere to the Single-Responsibility principle.

like image 40
flq Avatar answered Sep 24 '22 06:09

flq