3

I am writing a server to handle different types of messages from client to host. The messages are of varying content and length and will be preceded by an identifier to identify what type the message is. Some messages (of type A, B, and C for instance) should just be forwarded between machines. Others (of type D, E, and F for instance) require a specific action from the server: ie. they are not just forwarded.

(NOTE: The clients on either end need to differentiate between A, B, and C but the server only needs to know to forward those messages. Both the server AND the clients need to be able to differentiate between D, E, and F.)

What I am trying to find is a good Object Oriented paradigm for representing the idea of what "type" a message is. So to clarify, imagine the server receives a messaged labeled as "Type B", it needs to simply forward that to the appropriate client. However, if it receives a message labeled as "Type F" it will take different, specific, action. So how could I represent the "Type" in my code?

An ideal solution for what I want to implement would be something like this; and enumeration "Type" in which a certain subset of Commands are of the type "Forwardable". Obviously that isn't possible so I thought of a static class, Forwardable, that inherits from a base static class, Type. Again, impossible. Interfaces could be possible, but I would really rather not have to instantiate instances just to interpret a type.

The most straight forward way of having the server parse the message would be something like this:

byte[] payload = GetPayload((byte[])rawMessage);
Type message = GetMessageType((byte[])rawMessage);
if(message is Forwardable)
{
  ForwardMessage(payload);
}
else
{
  switch(message)
   case 1:
     //
     break;
   case 2:
     //
     break;
}

So finally, what is the proper OO way of representing a set of Types in which a subset of those Types is a Subtype?

EDIT: In a way, I feel like this should be done by defining a static class of "Types" and defining another static class that inherits from "Types" called "Forwardable". I can then cast what my server receives as a "Type" and say if(header is Forwardable). But unfortunately, you cannot inherit from static classes . . .

4

5 に答える 5

0

これは機能しますか?

enum ActionType
{
    Forwardable, ....
}

class Message //could be abstract, then make classes A : Message, B: Message etc with appropriate fields
{
    private Byte[] payload;

    public ActionType CurrentActionType {get;private set;}

    public Message(byte[] rawmessage)
    {
        //parse message's header to determine ActionType
        //parse rest of message and save to payload or other variables in inherited classes
    }
}

Message msg = new Message((byte[])rawMessage);
switch(msg.CurrentActionType)
{
    case ActionType.Forwardable :
        ForwardMessage(msg); break;
    case...
    case...
}

編集ShouldForwardコメントで説明されている機能:

private Boolean ShouldForward(Message msg)
{
    switch(msg.CurrentActionType)
    {
        case ActionType.A:
        case ActionType.B:
        case ActionType.C:
            return true;
        default:
            return false;
    }
}
于 2013-04-23T13:55:32.460 に答える