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). , .
- . . Reflection, , . , "" , . , , MEF, .
, , . if /, .
:
, , , , Message , , . , . RawMessage , " ".
DTO :
/ . , FactoryTakenOverByRobotsMessage , , . , , DTO (, ) , /. , , , , , - . , , XML .
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 }
# , , - , , # - . , - .
, . . 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).
Source: https://habr.com/ru/post/1750360/More articles:How can I request the Google Safebrowsing API to validate a URL using Python? - pythonHow can I use linq to select columns of a gear array - c #How to fix incorrect syntax near "INPUT" in SQL Server 2005 - sqlAndroid RTSP - MediaPlayer / PVMFFailure initialization failure - androidHighlight 64-bit numbers in x86 assembler? - assemblyVisual Studio 2010 vs Visual Studio 2010 Express file and entity model - visual-studioDelphi Imagelist: loading icons with ResourceLoad from .res - delphiOpinions on resolving a gaming platform / background conflict - c #How to manually create a DBRef using pymongo? - pythonHow to split the remaining horizontal space between two columns in an HTML table? - htmlAll Articles