1

I'm writing a desktop MMO game and I want to consult on the architecture question. I have such classes as NetworkManager, ClientWindow, CachingTextureAtlas they are needed only in one instance for the game and here is my question: is it correct to make them singletons? If yes it will be a kind of interaction through the global classes which is not good from the design point of view as it seems to me and if no we'll compose them in a facade and we'll have to pass those all to constructors of too many classes which is not convinient as well. What is a better choice?

4

1 に答える 1

5

シングルトンは慎重に使用する必要があります (ほとんど使用しないでください)。私は多くの場所を見てきました。最近のシナリオは休止状態のセッション ファクトリで、最初は単一のインスタンスで問題ないと考えていましたが、複数のインスタンスのシナリオに遭遇し、コードをリファクタリングすることになりました。他の問題は、コードの単体テストを作成している場合、これらのシングルトン クラスに依存するすべてのクラスにとって悪夢になることです。

解決策の1つは、これらのインスタンスを提供できる一種のファクトリを注入することです/それは一種の間接性を提供し、直接結合を回避します。もう 1 つのアプローチは、コンテナー (例: pico コンテナー) を用意し、コンストラクターの依存関係として注入することです。これは、あなたが指摘したものでもあります。

于 2013-03-26T07:54:35.697 に答える