質問
私は、次のような状況のための最高のデザインを探しています。
私たちは、多くのオブジェクトを持つインスタンス額縁のために、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を作成し、簡単に各1のモードを変更することができます。
今、私はすべてのPictureFramesのグローバルセッターを追加します。そして、それぞれの新しいPictureFrameは、デフォルトの一つとしてグローバル設定を取る必要があります。それは後でを通じてさまざまに設定することができます。
ここに私の解決策ですが、私はより良いものがある場合は議論したい。
私はすべてのPictureFramesが到達可能であるPictureFrameクラスに静的フィールドPictureFrame.Instancesを追加しました。今、私はすべてのフレームに新しいグローバルモードを適用するために、すべてのPictureFramesを反復することができます。
また、私はすべてのフレームにそれを変更し、PictureFrameのコンストラクタでそれを読めば、私は、グローバルモードに設定された第2の静磁場PictureFrame.GlobalImageModeを持っています。 GlobalImageModeのためのセッターも、PictureFrameクラスの静的することができます。
解決
ここだけ野生のショット...:なぜあなたはいつもそれで条件と現在のフレームモード用のゲッターを使用しないでください。
class PictureFrame {
private PictureMode instanceMode;
private static PictureMode? globalMode;
private PictureMode CurrentMode {
get {
return globalMode ?? instanceMode;
}
}
}
他のヒント
私が正しく、問題文を理解していれば、私は、これはあなたが必要なものに似ていると思います:
public class Face extends Image { }
public class Screen extends Image { }
public class PictureFrame {
private Image picture = null;
public PictureFrame(Image newPicture) {
this.setPicture(newPicture);
}
public setPicture(Image newPicture) {
this.picture = newPicture;
}
}
public class PictureFactory {
private static Image defaultPicture = null;
public static void setDefaultPicture(Image newPicture) {
PictureFactory.defaultPicture = newPicture;
}
public static Image getDefaultPicture() {
return PictureFactory.defaultPicture;
}
public static PictureFrame getNewPictureFrame() {
return new PictureFrame(PictureFactory.defaultPicture);
}
}
public class PictureFrameManager {
private static PictureManager INSTANCE = new PictureManager();
private Vector<PictureFrame> frames = new Vector<PictureFrame>();
public static PictureFrameManager getInstance() {
return PictureManager.INSTANCE;
}
private PictureFrameManager() {}
private void addPictureFrame(PictureFrame frame) {
this.frames.add(frame);
}
private void setFramesToDefault() {
Image defaultPicture = PictureFactory.getDefaultPicture();
Enumeration<PictureFrame> iFrames = frames.elements();
while(iFrames.hasMoreElements()) {
iFrames.nextElement().setPicture(defaultPicture);
}
}
}
あなたは経由して、それを使用します
Face face = new Face();
//...do something to load the face object here
PictureFactory.setDefaultPicture(face);
PictureFrame frame = PictureFactory.getNewPictureFrame();
PictureFrameManager manager = PictureFrameManager.getInstance();
manager.addPictureFrame(frame);
Screen screen = new Screen();
//...do something to load the screen object here
PictureFactory.setDefaultPicture(screen);
manager.setFramesToDefault();
あなたがイメージを拡張したくないか、複数のモードを持つようにしたい場合は、代わりに、あなたがイメージをラップし、それが何であるかのモードを言うためにデコレータオブジェクトを作成することができます。