Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Imagine I have a collection of object instances which represent activities for a user to undertake. Dependent on user attributes, I have to randomly select instances to present activities to the user.

For some users, I need to present more activities to them than there are available activities in which case, I want to use the following algorithm.

If all available activities have already been presented to the user, then re-select a "used" activity, selecting the earliest presented activity ordered by frequency of use. In other words, try to reduce repetition and where repetition is unavoidable, use the instances which have been repeated less often and were presented furthest back in time.

Before I go on to code that algorithm, I wondered if there is some existing pattern I can re-use?

[EDIT] "Furthest back in time" is not relevant as I will pass the algorithm an ordered collection of used instances where the first entry is the first presented.

share|improve this question
Does it actually make sense for the user to see those activities repeated? – Jan Hudec Oct 29 '12 at 9:41
Why shouldn't the instances be reused? Displaying them should be a non-modifying operation, no? – Jan Hudec Oct 29 '12 at 9:42
Your comments made me realise I've done a poor job of describing the problem. I'll think about a concise way to present it more clearly. Thanks. – Simon Oct 29 '12 at 11:21

Game Coding Complete has source for Pseudo-Random Traversal of a Set which sounds like what you need. It generates a random list of tasks from the set, but guarantees each task will be visited once before it starts generating the same task again.

share|improve this answer
Depending on the edition, this book has an incorrect pseudo-random traversal algorithm. Hopefully they fixed the issue in the newest edition. – Ivan Oct 30 '12 at 16:46

In class Activity implement Comparable interface, overwriting compareTo() method.

There you can write the logic to indicate when exactly an activity is less than or greater ( orderwise ) than other.

Then let java collections API sort the collection.

As you can see there's not need of two lists, one for unused and other for used activities, since the order will garantee that after all new activities, used ones according to their frequency of use will be selected.

share|improve this answer

There are users and activities.

Activities are displayed to users depending on user's attributes.

Fundamentally, you have an algorithm (strategy) for displaying activities to the user.

First step is to encapsulate your algorithm somewhere. I propose to create a class called UserActivityStrategy for storing this algorithm.

You then get your user, get their previous activities and pass those objects to the strategy which will execute your algorithm and return relevant data.

var user = new User();
var previousActivities = new ActivityRepository().GetPreviousActivitiesForUser(user);

var proposedActivities = new UserActivityStrategy(previousActivities, user).GetActivities();

I don't like the fact that I'm passing both previousActivities and user to the strategy. You could refactor this by encapsulating both in a separate class called UserActivityHistory. This class would encapsulate user and their previous activities.

This one of many ways of solving this problem.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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