locked
Pattern to choose RRS feed

  • Question

  • Hi,

    I have worked for several years as a developer/analyst and now I am trying to improve my design knowledge... I hope my question is right for this forum...

     

    I have a very large XML file (in excess of 350Mb and can probably get even bigger) that contain some hierarchical data (nested elements). I also have the XML schema that specifies the allowed hierarchical relation between the various elements (well, a schema…). This imply, for performance reasons that I have to use a XML reader of some type.

     

    Writing a "prototype" I ended up with a class that contains the reader and has several HandleElementXXX function that work on the reader… Starting from a main loop (while (!reader.EOF) the reading/parsing is delegated to the various functions, each further delegate reading. The function returns a "generic" data object that store the information of the element and point to child elements (by delegating processing to HandleXXX functions)

    The code is something like:

     

    Class DataObject

    {

                List<DataObject> childObjects; //Store the child objects

                ArrayList attributes;        //Store the attributes

    }

     

    Class Main

    {

                void Process()

    {

    XmlReader reader

    DataObject root

    Open file à reader

    while( ! reader.EOF )

    {

    Switch reader.Name

    {

    case Element1:

    root.addChild(HandleElement1());

    case Element2:

    root.addChild(HandleElement2());

                                        }

                            }

    }

     

                DataObject HandleElement1()

                {

                            While(! reader.EOF && !finishedElement1())

    {

    Switch reader.Name

    {

    case Element1_1:

    root.addChild(HandleElement1_1());

    case Element1_2:

    root.addChild(HandleElement1_2());

                                        }

    }

     

                }

    }

     

    The code, the way I see it is quite procedural and does not conform to OO methodologies…

     

    Now I am about to write the actual implementation and I want to do it using better techniques… I thought that such situation is very similar to state pattern but not sure and I am not that experienced with this and other patterns…

    Very interesting to me not only the final solution but also the thought process when approaching such task…

     

    Also, I failed to mention before but eventually the DataObject attributes need to be stored in database after adding some parent identifier fields into child elements…

     

    Tuesday, July 25, 2006 10:18 PM

All replies

  • To manage hierarchy of data, we normally use the composite pattern.

    You can find articles on this in Dofactory.com website

    Tuesday, August 8, 2006 11:50 PM
  • You can have a separate handler classses and then either have the reader class raise an event (e.g. data ready) and have the a handlers register to handle it or you could inject the handlers to the reader and have it call them one by one.  You may want to check out the Asyclic Visitor pattern http://www.objectmentor.com/resources/articles/acv.pdf

    Arnon

    Sunday, August 20, 2006 4:29 PM