mercredi 4 novembre 2015

Lots of Sub Classes or a few Builder classes?

I have a 'message' class which is essentially a wrapper around a byte[] with a bunch of utility functions for adding data into the byte[].

var msg = new MyMessage();
msg.Insert('This String');
msg.InsertSpecialChar(1);

I am now at the point of creating the code which builds the messages and I cant decide the better approach between creating a child class per message:

internal class ConfigListMessage : MyMessage
{
    public ConfigListMessage()
    {
        Insert(X_CONFIG_LIST);
        .......
    }
}

Or a series of 'builder' classes

internal class SystemMessages
{
    public MyMessage ConfigListMessage()
    {
        var msg = new MyMessage();
        Insert(X_CONFIG_LIST);
        ........
        return msg;
    }

    public MyMessage CreateAccountMessage(string username, string password)
    {
        var msg = new MyMessage();
        Insert(username);
        ........
        return msg;
    }
    ..................
}

I expect this structure to grow a bit, though it already has more than 300 possible messages.

Which style would make more sense?

Thoughts

  • There will be A LOT of child classes
  • Child classes mean that I can add messages without affecting other developers.
  • It seems awkward having classes that only fill an array.

Bonus Question

Each of these messages have a response which reverses this process turning a byte[] into some data class.

If the child class approach is suggested would it also make sense that a ConfigListMessage also know how to parse its response or should that be handled by a new class type of MyMessageResponse?

Aucun commentaire:

Enregistrer un commentaire