Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:51:41+00:00 2026-05-31T00:51:41+00:00

I am working on a parser for a serial data protocol. I have an

  • 0

I am working on a parser for a serial data protocol. I have an overarching Packet class, a couple sub-classes such as CommandPacket and StatusPacket, and then a few sub-classes of each of those:

  • Packet
    • CommandPacket
      • CommandTypeA
      • CommandTypeB
    • Status Packet
      • StatusTypeA
      • StatusTypeB

The Packet class has a static method for taking data off of a buffer, and if it represents a valid packet, returning a new instance of the Packet class, instantiated with the necessary data.

Now, this is where my problem comes up. I would like to be able to return the most specific type of packet. To facilitate this, I have created a static .isValid() method, which is overridden on the sub classes. The idea is that I could loop through each type of specific packet (CommandTypeA, CommandTypeB, StatusTypeA, StatusTypeB, etc.) calling .isValid() until one of them returned TRUE. At that point, I would return a new instance of that specific packet type.

While of course it is possible for me to create this method directly, how do I account for types of packets that are not added to my project yet? I want someone to be able to extend my class in the future without having to modify the original Packet class.

I have considered utilizing reflection for this, but am avoiding it, since these methods would be called for each and every packet received, and must be efficient.

Any thoughts on how I should re-work my design pattern?


While I don’t think it is entirely relelvant to the discussion, I am doing this in VB.NET. There is also a similar (but not quite the same) question posted here: Java – subclass validation design pattern

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:51:42+00:00Added an answer on May 31, 2026 at 12:51 am

    Based on the comments and MarkJ’s lead, here’s a suggestion (which is eventually based on an interface instead of an attribute for stronger type checking):

    public interface IPacketValidator
    {
        bool IsPacketValid( Packet packet );
        Type PacketType { get; }
    }
    
    public class ValidatorsRegistry
    {
        private List<IPacketValidator> m_validatorsList;
    
        private static ValidatorsRegistry m_Instance = new ValidatorsRegistry();
        public static ValidatorsRegistry Instance { get { return m_Instance; } }
    
        private ValidatorsRegistry()
        {
            InitValidatorsRegistry();
        }
    
        private void InitValidatorsRegistry()
        {
            m_validatorsList = new List<IPacketValidator>();
    
            Type iPacketValidatorType = typeof( IPacketValidator );
    
            foreach ( Assembly asm in AppDomain.CurrentDomain.GetAssemblies() )
            {
                foreach ( Type type in asm.GetTypes() )
                {
                    if ( iPacketValidatorType.IsAssignableFrom( type ) && type != iPacketValidatorType )
                    {
                        IPacketValidator validator = (IPacketValidator)Activator.CreateInstance(type);
                        m_validatorsList.Add( validator );
                    }
                }
            }
        }
    
        public Type GetSpecificPacketType(Packet packet)
        {
            Type packetType = typeof( Packet );
            foreach ( IPacketValidator validator in m_validatorsList )
            {
                if ( validator.IsPacketValid( packet ) )
                {
                    packetType = validator.PacketType;
                    break;
                }
            }
    
            return packetType;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started working on a simple XML pull-parser, and as I've just defuzzed
I have a working statement in PHP using the simple dom parser that I
I'm working on a project for class in which we have to build a
I have written a working token parser based on the code shown at spirit
I have written a perfectly working XML parser using PHP... using, $xml = simplexml_load_file($newfile);
I have built an event-base java html parser: Lagarto . Now I am working
Quite a funny question I have. I am working now on the HTML parser
I'm working with a few Perl packages, we'll call them Some::Parser and Some::Data .
I'm working on a parser in php which is designed to extract MySQL records
I'm currently working on a parser for our internal log files (generated by log4php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.