0

I am working on GUI app (simple game), where one of the objects (Let us call it Object A) uses images that I load directly. I am implementing method loading images on game start so that I do not have to reload files every time I reconfigure the game etc. That method loads all necessary images as an array, then another method (BufferedImage[] getImages()); returns this array. The class of which this method is (Object B, a JPanel), draws Object A, which in turn, was instantiated by Object C (JFrame, which, of course, also instantiates object B).

I want to know if I can access Object B’s getImages() method directly from the Object A’s method without passing a reference via method call. Is it entirely possible (via ClassPath etc.), and would it be good programming practise to do so?

theUg
  • 1,284
  • 2
  • 9
  • 14

2 Answers2

0

It sounds like you're looking for a singleton pattern. Do this:

public class ImageContainer {
    private final BufferedImage[] images = null;

    private static ImageContainer _instance = new ImageContainer();

    // NOTE PRIVATE CONSTRUCTOR
    private ImageContainer() {
        BufferedImage[] images = loadImages();
    }

    public static ImageContainer getInstance() {
        return _instance;
    }

    private BufferedImage[] loadImages() {
        // Do the loading image logic
    }

    // You might not want this in favor of the next method, so clients don't have direct access to your array
    public BufferedImage[] getImages() {
        return images;
    }

    public BufferedImage getImage(int index) {
        return BufferedImage[i];
    }
}

Then, whenever you need an image, just do

ImageContainer.getInstance().getImage(3);

You could even use an EnumMap instead of an array to make it easier to know what image to return in your code.


By the way, you can read an excellent discussion on the different reasons when you would and would not use a static method here.

Community
  • 1
  • 1
durron597
  • 31,968
  • 17
  • 99
  • 158
  • 1
    @theUg: Dependency injection is wonderful when you need it (potential change of a data source, for example)... And it's good to build early in a project's lifestyle because it's really tough to refactor in... But totally unnecessary for a homework assignment :) – durron597 Nov 29 '12 at 12:12
0

You can call B's getImages() method without having a reference only if getImages is a static method. This may or may not be a good idea, depending on your situation.

Another option is to make B a "singleton" class. You can do that approximately like this:

public class B {
  private static B theInstance;
  private bufferedImage[] images;
  private B() {
  }

  public static B getInstance() {
    if(theInstance == null) {
      theInstance = new B();
    }
    return theInstance;
  }

  public BufferedImage[] getImages() {
       if(images == null) {
            /* get the images */
       }
       return images;
  }
}

Note, however, that singletons are frowned upon by some folks. The alternative is dependency injection.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • So, would the static method do the same thing as singleton class (for the purposes of my application) as it produces single object as well? What would be a better way, given the singleton concerns, or your warning about how static method may or may not be a good idea? – theUg Nov 28 '12 at 23:31
  • @theUg: I edited a good link about static vs singleton into my answer above – durron597 Nov 28 '12 at 23:44
  • @durron597 - that's what well-known patterns and idioms are all about - it makes it easier for us to read and understand each other's code :-) – GreyBeardedGeek Nov 29 '12 at 00:38
  • @theUg - I personally think that many of these discussions are nit-picking, and a single static method vs. a singleton is largely a matter of style. The big win is with dependency injection, which provides the means to easily unit test other components that would otherwise be tightly coupled to the singleton - the test class can inject a mock into the class under test (instead of the 'real' class). – GreyBeardedGeek Nov 29 '12 at 00:43
  • @durron597 And what about my other option (passing reference to the result of those methods in a constructor of methods of Object A)? How does that compares to another two from the standpoint of OOP, and other considerations (maintenance, refactoring etc.). Again, I am not going to lose sleep over it (school project), but for the sake of learning? – theUg Nov 29 '12 at 07:28
  • @GreyBeardedGeek Why are singleton classes frowned upon? – pratnala Mar 05 '14 at 09:16
  • @pratnala Mostly because of over-use of the pattern. They can also be a bit inflexible. But they are still useful in some situations. – GreyBeardedGeek Mar 07 '14 at 01:19