1

I've got a ListActivity with about 100 events. (These events are also displayed on a map in another activity.)

So I've my MyListActivity, which handles the list and a MyListAdapter to populate the list that deals with MyEvent-Objects. As model I have a MyEvent-model-Class and a MyEventStorage-Class.

Now have written a method to return an image for an event based on its ID. It does some decisions which image to load, where it gets the image from, loads it and resamples it.

Where should I put this method in best practice?

  • I don't want to copy it in every activity where it is needed but just in one place.

  • I'd like to have it in my MyEvent-Class, so I can call myEvent.getImage(); but it somehow feels wrong to put this method inside the model class with all the getters and setters. Is it wrong?

  • Should I write a helper class containing this method? As a static method? Would this still provide a good performance?

  • Or maybe create an additional MyImageGetter-object for every MyEvent-object?

  • Or expand the MyEvent-model with an image-variable and getters/setter and create an extra class that puts the proper image in the model? How would I call that method?

  • Another solution?

MyEvent.java:

public class MyEvent {
    private int id;
    private int category;
    private String eventname;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    // other getters and setters
}

MyEventStorage.java:

private static MyEventStorage instance = null;

    private List<MyEvent> store;

    private MyEventStorage() {
        store = new ArrayList<MyEvent>();
    }

    // get the storage containing the events
    public static MyEventStorage getInstance() {
        if (instance == null) {
            instance = new MyEventStorage();
        }
        return instance;
    }

    public List<MyEvent> getStore() {
        return store;
    }

    public void setStore(List<MyEvent> store) {
        this.store = store;
    }

     // Add a Event to the store
    public void addEvent(MyEvent myEvent) {
        store.add(myEvent);
    }
    // Remove a Event from the store
    public void removeEvent(MyEvent myEvent) {
        store.remove(myEvent);
    }
}

The method I want to integrate:

Image getImageById(int id) {

    // decide which image to load based on the events id

    // decide where to load the image from

    // check if image available

    // load image if available else load placeholder image

    // resample image

    return image;
}

Thank you advance!

A2i_
  • 398
  • 3
  • 13

1 Answers1

0

I think your last bullet point is spot on.

If the Image is in fact a property of MyEvent, it makes sense to add an instance variable to that class. You shouldn't include the logic for retrieving an event's image from a datasource in the model, but rather use a static utility method to load this property.

Your getImageById method looks like it has to do a decent amount of work to retrieve the image from wherever it is stored. I think it would make the most sense to create a utility class (along the lines of ImageRetriever) like you mentioned in order to perform the actual retrieval of the image. This prevents you from having to copy the method to multiple places. Performance should not be a concern either, as you'll never have to instantiate this class.

The code could look something like this:

public class MyEvent {
   private int id;
   private int category;
   private String eventname;
   private Image image;

   public MyEvent(int id...) {
      // initialize instance vars
      setImageFromRetriever();
   }

   public void setImage(Image image) {
      this.image = image;
   }

   public void setImageFromRetriever() {
     // optional null check if you don't want to reload images
     setImage(ImageRetriever.getImageById(this.id));
   }
}
Ben Siver
  • 2,758
  • 1
  • 25
  • 42
  • Thank you, I just did so and it works quite good, except that I need to access a `Context` inside the static method in order to get the resources. Neither the ImageRetriever nor the model class `MyEvent` knows about any context... How would you solve that? Thank you! – A2i_ Oct 30 '13 at 22:30
  • Hm, that's a good point. Check out [this answer](http://stackoverflow.com/a/5114361/491897) for a proposed solution. This solution seems kind of like a hack to me, but apparently it's a pretty common one. By the way, if my answers helped out, please consider accepting it. Best of luck to you! – Ben Siver Oct 31 '13 at 02:13