Design pattern for loading multiple message types

As I was browsing through SO, I came across a question about handling several types of messages . My concern is how can I accurately download such a message? I decided to have a separate class with a method that loads one message each time it is called. This method should create a new instance of a specific message type (e.g. AlphaMessage, BetaMessage, GammaMessage, etc.) and return it as a message.

class MessageLoader
{
    public Message Load()
    {
        // ...
    }
}

The code inside the method is something that looks very horrible for me, and I would really like to reorganize / get rid of it:

Message msg = Message.Load(...); // load yourself from whatever source
if (msg.Type == MessageType.Alpha) return new AlphaMessage(msg);
if (msg.Type == MessageType.Beta) return new BetaMessage(msg);
// ...

In fact, if the whole design looks too dirty and you have a better solution, I am ready to restructure all of this.

, , , . .

Edit: , , ( , ), ( , msg Data). , .

+3
4

- . . Reflection, , . , "" , . , , MEF, .

, , . if /, .


:

, , , , Message , , . , . RawMessage , " ".

DTO :

/ . , FactoryTakenOverByRobotsMessage , , . , , DTO (, ) , /. , , , , , - . , , XML .

+1

CkH , Factory . . , Factory. , .

class Class12
{
    public static void Main()
    {
        Message m = new Message(1, "Hello world");
        IMessageHandler msgHandler = Factory.GetMessageHandler(m.MessageType);
        msgHandler.HandleMessage(m);

        Message m2 = new Message(2, "Adios world");
        IMessageHandler msgHandler2 = Factory.GetMessageHandler(m2.MessageType);
        msgHandler2.HandleMessage(m2);
    }
}
public class Factory
{
    public static IMessageHandler GetMessageHandler(int msgType)
    {
        IMessageHandler msgHandler = null;
        switch(msgType)
        {
            case 1: 
                msgHandler = new MessageHandler1();
                break;
            case 2: 
                msgHandler = new MessageHandler2();
                break;
            default: 
                msgHandler = new MessageHandler1();
                break;
        }
        return msgHandler;
    }
}
public class Message
{
    public int MessageType { get; set; }
    public string AMessage { get; set; }

    public Message(int messageType, string message)
    {
        this.MessageType = messageType;
        this.AMessage = message;
    }
}
public interface IMessageHandler
{
    void HandleMessage(Message m);
}
class MessageHandler1 : IMessageHandler
{

    #region IMessageHandler Members


    public void HandleMessage(Message m)
    {
        string message = m.AMessage;
        Console.WriteLine(message);
    }

    #endregion
}
class MessageHandler2 : IMessageHandler
{

    #region IMessageHandler Members


    public void HandleMessage(Message m)
    {
        string message = m.AMessage;
        Console.WriteLine("Hey there " + message);
    }

    #endregion
}
+3

# , , - , , # - . , - .

0

, . . AlphaMessage BetaMessage , .

if (msg.Type == MessageType.Alpha) return msg as AlphaMessage;
else if (msg.Type == MessageType.Beta) return msg as BetaMessage;

Consumer code should handle the case when a failure occurs ( because it returns null).

0
source

Source: https://habr.com/ru/post/1750360/


All Articles