以下の状況に最適なデザインを探しています。
たとえば額縁など、1 つのクラスから多くのオブジェクトがあります。これで、各額縁に 3 種類の画像を表示できるようになりました。1) 顔 2) スクリーンショット 3) 空っぽ
簡単だ:
public enum PictureMode
{
Face,
Screen,
None
}
public class PictureFrame {
private PictureMode mode;
public PictureMode Mode
{
get { retrun mode; }
set { /* set currentPicture to the correct one */ }
}
private Image currentPicture;
private Image face;
private Image screen;
private Image empty;
public PictureFrame(Image face, Image screen) {
this.face = face;
this.screen = screen;
mode = PictureMode.None; // Maybe this is our default.
}
}
これで、異なる画像でいくつかの PictureFrames を作成し、それぞれのモードを簡単に変更できるようになりました。
ここで、すべての PictureFrames にグローバル セッターを追加したいと考えています。次に、新しい各 PictureFrame は、グローバル設定をデフォルトとして使用する必要があります。後で別のスルーに設定できます。
これが私の解決策ですが、より良い解決策があるかどうかについて話し合いたいと思います。
すべての PictureFrame に到達できる PictureFrame クラスに静的フィールド PictureFrame.Instances を追加しました。これで、すべての PictureFrames を繰り返し処理して、新しいグローバル モードをすべてのフレームに適用できます。
In addition I have a second static field PictureFrame.GlobalImageMode where I set the global mode if I change it on all Frames and read it in the Constructor of the PictureFrame. The setter for the GlobalImageMode can be static in the PictureFrame class, too.