The app I'm developing needs to display lists of items. Simple enough, but there are a number of things which can change based on user input:

  • Items can be added to/removed from the list.
  • The items in the list can be updated. Multiple items can be updated simultaneously!
  • The filters can be updated. Multiple filters can be updated simultaneously!
  • The sort order of the items can change.

Possibly relevant info:

  • It's a multiplatform Java app.
  • One of the platforms we distribute on is MIDP, so that limits which language features/libraries/etc we can use.
  • The implementation is currently split into a model class (in the common code) and device-specific view/controller classes.

I've tried a couple of approaches to designing a suitable list implementation, but both have drawbacks:

Approach 1:

The platform-specific controller implementations are responsible for calling a refresh() method whenever anything happens which could change the list.

This approach keeps the list class (model) implementation fairly straightforward, but leaves much of the responsibility up to the platform-specific controller implementations. The more logic there is in these classes, the greater the chance of bugs creeping in. It also encourages the controllers to directly notifying the view of updates, which feels like poor design.

Approach 2:

The model is entirely responsible for raising events to notify the platform-specific views of changes. The list items, filter, and sorters notify the model when they change, which then notifies the views of changes.

This keeps the platform-specific classes much simpler, and feels like a stronger design (less coupling between the classes, etc). However, the list class implementation becomes much more complicated.

My main problem with this approach is dealing with a proliferation of events when multiple items/filters are updated in a single operation.

My questions:

  1. Is there other approaches I could/should be trying?
  2. Is there a recognised way to manage/avoid the explosion of events in approach #2?

2 Answers 2

  1. As the lists are the model (and the items are only parts of them), I would suggest the following process:

    • When changed, items raise events to the list in which they are placed.
    • When changed (directly or via a change in the items), lists raise events to the depending views.
    • Lists filtering and sorting only concern the views. So the sorters and filters are not longer active elements but just passive elements manipulated by the views.
  2. I don't know. The fact to restrict the filtering and sorting activities to the views themself will reduce the number of events sent. The single other way I can imagine for reducing the number of events sent is to be as specific as possible so that only the concerned receivers receive an event. An alternative could be to gather the events in a "super-event" received by "something" acting consequently, but I fear it is not conceptually a better solution.

  • Very good points re. Q1: separating out the filtering/sorting behaviour has made things much more manageable. I may repost Q2 as a separate question in future, in the hopes of it getting more attention. Commented Jan 29, 2013 at 10:04

As you will be continually adding and removing items from the list I'd suggest you use a LinkedList as this provides more efficiency by simply removing reference from the removed item to the next available in the list - thus preventing the need to move all elements forward. In the case of filtering you could implement the Criteria Design Pattern. First we declare an Interface with a single abstract method:

public interface Criteria {
    * abstract method applyFilter
    * @param object to check for
    * @param users
    * @return filtered list
    List<User>applyFilter(Object object, List<User>users);

Next we create a concrete filter class which implements this interface. For instance we want to filter out all users who are under 25 years of age.

public class CriteriaAge implements Criteria {

    public List<User> applyFilter(Object object, List<User> users) {
        Integer age = (Integer) object;
        List<User>filterList = new ArrayList<>();
        for(User u : users)
            if(u.getAge() < age)
        return filterList;

To apply this filter we can do:
// from the Controller (contains the complete list of users from DB)

Criteria ageCriteria = new CriteriaAge();

// populate the View with presented data


// returns a list of users under 25 and prints this out.

This example uses Model-View-Controller and Filter (Criteria) Design Patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.