tags:

views:

70

answers:

3

Hi all, as a beginner, I have formulated some ideas, but wanted to ask the community about the best way to implement the following program:

It decodes 8 different types of data file. They are all different, but most are similar (contain a lot of similar fields). In addition, there are 3 generations of system which can generate these files. Each is slightly different, but generates the same types of files.

I need to make a visual app which can read in any one of these, plot the data in a table (using datagridview via datatable at the moment) before plotting on a graph.

There is a bit more to it, but my question is regarding the basic structure.

I would love to learn more about making best use of object oriented techniques if that would suit well.

I am using c# (unless there are better recommendations) largely due to my lacking experience and quick development time.

I am currently using one class called 'log' that knows what generation/log type the file that is open is. it controls reading and exporting to a datatable. A form can then give it a path, wait for it to process the file and request the datatable to display.

Any obvious improvements?

A: 

Ok, first thing - make one class for every file structure type, as a parser. Use inheritance as needed to combine common functionality.

Every file parser should have a method to identify whether it can parse a file, so you can take a file name, and just ask the parsers which thinks it can handle the data.

.NET 4.0 and the extensibility framework can allow dynamic integration of the parsers without a known determined selection graph.

The rest depends mostly on how similar the data is etc.

TomTom
A: 

Okay, so the basic concept of OOP is thinking of Classes etc as Objects, straight from the offset, object orientated programming can be a tricky subject to pick up at first but the more practice you get the more easy you find it to implement programs using OOP.

Take a look here: http://msdn.microsoft.com/en-us/beginner/bb308750.aspx

So you can have a Decoder class and interface, something like this.

interface IDecoder
{
    void DecodeTypeA(param1, 2, 3 etc);
    void DecodeTypeB(param1, 2, 3 etc);
}

class FileDecoder : IDecoder
{
   void DecodeTypeA(param1, 2, 3 etc)
   {
      // Some Code Here
   }

   void DecodeTypeB(param1, 2, 3 etc)
   {
      // Some Code Here
   }

}
kyndigs
A: 

As you have realised there is a great deal of potential in creating a very elegant OOP application here.

Your basic needs - as much as I can see from the information you have share - are:

1) A module that recognises the type of file

2) A module that can read the file and load the data into a common structure (is it going to be common structure??) this consists of handlers

3) A module that can visualise the data

For the first one, I would recommend two patterns:

1a) Factory pattern: File is passed to a common factory and is parsed to the point that it can decide the handler

2a) Chain-of-responsibility: File is passed to each handler which knows if it can support the file or not. If it cannot passes to the next one. At the end either one handler picks it up or an error will occur if the last handler cannot process it.

For the second one, I recommend to design a common interface and each handler implements common tasks such as load, parse... If visualisation is different and specific to handlers then you would have those set of methods as well.

Without knowing more about the data structure I cannot comment on the visualisation part.

Hope this helps.

UPDATE

This is the factory one - a very rough pseudocode:

Factory f = new Factory();
ILogParser parser = f.GetParser(fileName); // pass the file name so that factory inspects the content and returns appropriate handler
CommonDataStructure data = parser.Parse(fileName); // parse the file and return CommonDataStructure. 
Visualiser v = new Visualiser(form1); // perhaps you want to pass the refernce of your form
v.Visualise(data); // draw pretty stuff now!
Aliostad
I see.. so how would you reference this from the form? At the moment, I have variable within the class pointing to 'log'. With different classes, would you have 8 different variables, or is there a way to refer to any one of them from the same variable?
Mark
I have updated.
Aliostad
Many thanks. So.. just to be clear, the 'Factory' is a general class that can tell which is which.. 'ILogParser' is just an interface, which is common to all different log classes.. and 'CommonDataStructure' could be a DataTable..?
Mark
Absoultely. I think you got it all.
Aliostad
Thanks! Very helpful.
Mark
Just one more quick thing.. so parser is static? And how would you pass return the parser, or would Factory instantiate parser and then just return the reference to it?
Mark
Parser is not static and it is initialised by the factory. Have a look at wiki for Factory design pattern. Factory can be made singleton.
Aliostad