views:

79

answers:

2

I'm looking for the best Design for the following situation.

We have many objects form one class, for instance a picture frame. Now each of the picture frames can display 3 types of picture. 1) a face 2) a screenshot 3) empty

Thats easy:

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.
    }
}

We can now create some PictureFrames with different pictures and easily change the mode for each one.

Now I want to add a global setter for all PictureFrames. Then each new PictureFrame should take the global setting as the default one. It can later be set to an different through.

Here is my solution, but I want to discuss if there is a better one.

I added a static field PictureFrame.Instances to the PictureFrame class where all PictureFrames are reachable. Now I can iterate over all the PictureFrames to apply the new global mode to all frames.

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.

A: 

If I understand the problem statement correctly, I think this is similar to what you need:

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);
    }
  }
}

You use it via:

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();

Alternately, if you don't want to extend Image or you want to have multiple modes, you could create a decorator object to wrap the image in and say what mode it is.

Jeremy Stanley
+1  A: 

Just wild shot here...: Why don't you always use getter for current frame mode with a condition in it:

class PictureFrame {
  private PictureMode instanceMode;
  private static PictureMode? globalMode;

  private PictureMode CurrentMode {
    get {
       return globalMode ?? instanceMode;
    }
  }
}
Rashack