1

I am writing an application in C#. Now i am thinking over and over again about its design. Have already changed my mind 3 or 4 times but thankfully for the good.

After few iterations i come up with a solution but i am still wondering what is the best way to achieve that with C#.

Basically i will have a class lets call it MessageManager, and after each action different classes will send a message to MessageManager and MessageManager will send the message depending on the response. Then i will have another manager call it UIManager it will perform all the UI switching or inform the MessageManager in case of any core/helper operation is required.

Now the thing is messages could make up to like 50-60 types each will have different type of arguments. And i want to design it in a way if i have new messages in future it can accommodate that as well.

What is the best way to accomplish that in C# like what will be the best for such case delegates, events. Flexibility is the most important thing.

4

1 に答える 1

1

オブザーバー パターン (パブリッシュ/サブスクライブ ロジック) とメディエーター パターンを組み合わせることで、問題を解決できると思います。Mediator クラスは Event Manager として機能します (ほとんどのクラスは、相互に依存するのではなく、メディエーターとして依存します)。

public class MessageManager{

    private Dictionary<string,List<MessageListener>> listeners;

    public void sendMessage(Message m){
        //loop over listeners of m
    }

    public void addMessageListener(MessageListener ml){
         //add a listener
    }

    public void removeMessageListener(MessageListener ml){
         //remove a listener
    }
}

MessageMessageManagerこのレベルで一般的な抽象化を行うことは、50 ~ 60 種類のメッセージを区別して維持するのが困難になることを回避するため、非常に重要です。の特定のサブタイプに依存することの特異性はMessage、より低いレベル、つまり直接の消費者に移動する必要があります。

于 2013-09-29T14:27:57.797 に答える