Results 1 to 5 of 5

Thread: Design Patterns... [Resolved]

  1. #1

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Resolved Design Patterns... [Resolved]

    Lets say I have a Class Named DataChunk each data chunk class has a header, this header has some basic Information on what the DataChunk contains.

    Based on the name that Header Contains I need to figure out what Parsing class should be created and then add it to a collection

    This is a basic version of the class

    VB Code:
    1. Interface IParseable
    2.  
    3. Sub Read(byref Reader as BinaryReader)
    4. Sub Write(byRef Writer as BinaryWriter)
    5.  
    6. End Interface

    VB Code:
    1. Public Class DataChunk
    2.     Implements IParsable
    3.  
    4.     Public Sub New(byRef Reader as BinaryReader)
    5.          Read(Reader)
    6.     End Sub
    7.  
    8.     Private _Header as ChunkHeader
    9.  
    10.     Public Sub Read(byRef Reader as BinaryReader)
    11.         _Header = New ChunkHeader(Reader)
    12.        '
    13.     End Sub
    14.  
    15.     'Write sub here (but not really relevant)
    16.  
    17. End Class

    ChunkHeader also Implements IParseable.

    basicly There are many different types of dataChunk's, the Header's name property is what should be used to decide what class should be used to continue parsing.

    These data chunks need to be stored in a typed collection so I was thinking of Inheriting Declaring dataChunk as MustOverride...

    eg..

    VB Code:
    1. Public Class DataChunk
    2.     Implements IParsable
    3.  
    4.     Public Sub New(byRef Reader as BinaryReader)
    5.          Read(Reader)
    6.     End Sub
    7.  
    8.     Private _Header as ChunkHeader
    9.  
    10.     Public Sub Read(byRef Reader as BinaryReader)
    11.         _Header = New ChunkHeader(Reader)
    12.         ParseData(Reader)
    13.     End Sub
    14.  
    15.     Public Sub Write(byRef Writer as BinaryWriter)
    16.        _Header.Write(Writer)
    17.        RenderData(Writer)
    18.     End Sub
    19.  
    20.     Public MustOverride Sub ParseData(byRef Reader as BinaryReader)
    21.     Public MustOverride Sub RenderData(byRef Writer as BinaryWriter)
    22.     Public MustOverride Function CanParse(DataChunkName as String)
    23.  
    24. End Class

    but then how do I dynamicly create an instance of the proper class only knowing the DataChunkName
    Last edited by <ABX; May 31st, 2005 at 03:59 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  2. #2

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Design Patterns...

    I'm still completely clueless...
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Design Patterns...

    Well I came up with a partial solution:

    I created another interface for (IDataChunk)
    ParseData, RenderData, CanParse.

    Now how should I enumerate the parsing classes?

    I could use reflection to get a array of all the types that Implement IDataChunk, but speed is an issue in the project.

    I could hard code the List of parsing classes into the application, but It just seems.... un-elegant. (if this is the BEST method for speed/maintenance, I will do it this way).

    Any other suggestions?

    Edit: Never Mind... I jumped the gun.
    Last edited by <ABX; May 30th, 2005 at 05:32 PM.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  4. #4
    Fanatic Member
    Join Date
    May 2002
    Posts
    746

    Re: Design Patterns...

    In pattern speak, sounds "Factory"-ish. I know the idea of a [possibly] long select case sounds inelegant, but it's often the way to go - and more of an abstraction than it might seem at first blush. Look here - might get you on your way (seem like you probably want the simple factory - not [necessarily] the abstract one).

  5. #5

    Thread Starter
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    Re: Design Patterns...

    I think I will need the abstract factory (the version I posted here was simplifed). Don't have time to implement it now, but thanks.
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width