私の現在のプロジェクトでは、ビデオ ストリーミング、オーディオ ストリーミング、特定の種類のデータ ストリーミングなど、あらゆる種類のストリーミング オプションを実行できるかなりファンキーなデバイスを使用しています。
各デバイスは、これらの各ストリームの限られた数のみをサポートします。議論のために、2 つのビデオ ストリームと 1 つのオーディオ ストリームをサポートできると仮定します。
以下のように設計しました。(ほとんどのアプリケーション ロジックは省略されています。)
public class FunkyDevice
{
int openVideoStreams;
int openAudioStreams;
public VideoStream openVideoStream(int id)
{
if (openVideoStreams < 2)
{
openVideoStreams++;
return new VideoStream(...);
}
}
public AudioStream openAudioStream(int id)
{
if (openAudioStreams < 1)
{
openAudioStreams++;
return new AudioStream(...);
}
}
}
ただし、今は複数のデバイスをサポートする必要があります。これらをユーザー セッションにグループ化します。すべてのユーザー セッションは、各ストリームの数も制限しますが、もちろん、これらの数はデバイスの制限と同じではありません (そうでなければ、問題は簡単すぎます)。たとえば、3 つのビデオ ストリーム (1 つ、2 つ、または 3 つの異なるデバイスのいずれか) と 1 つのオーディオ ストリームを持つことができます。
これに対処するための私の最善の試みは次のとおりです。
public class UserSession
{
int openVideoStreams;
int openAudioStreams;
public VideoStream openVideoStream(int deviceId, int id)
{
if (openVideoStreams < 3)
{
openVideoStreams++;
return getDevice(deviceId).openVideoStream(id);
}
}
public AudioStream openAudioStream(int deviceId, int id)
{
if (openAudioStreams < 1)
{
openAudioStreams++;
return getDevice(deviceId).openAudioStream(id);
}
}
}
ご覧のとおり、 と のパブリック インターフェイスFunkyDevice
はUserSession
ほぼ同じですが、 のすべてのメソッドにUserSession
追加のパラメータ がありますdeviceId
。私の実際のアプリケーションでは、2 つ以上の異なる種類のストリームがあり (また、ストリームを閉じるなどの他の操作も実行したい)、インターフェイスが非常に大きくなります。
このコードの重複を導入せずにこれを容易にするためのより良いパターンはありますか?