Task Filter Using The Strategy Pattern

I have an application that deals with a type Task, there are many ways to retrieve a task such as, getting all tasks, lookup a single task by its id, get all tasks that belong to a specific user and have a status of complete. As one may already notice, the combinations can be many. Too simplify, I will not allow so many variations; in addition, I will not code the familiar Dao interface containing the methods findById, findAll, findByUserIdAndCompleteStatus. Instead I will extract these methods into a Filter Spec interface as shown.

Strategy Pattern

Gof Strategy Pattern

The Context in this instance is the ApplicationService which has a Strategy and receives the Strategy during construction as follows:

ApplicationService service = new ApplicationService(new IdFilter(new IdFilterSpec(“1001”)));

public class ApplicationService {

private Filter filter;

public ApplicationService(Filter filter) {   this.filter = filter;  }

public List<Item> filterContext() {
return filter.filter();


The filter receives a specification that matches the concrete filter for its data in, the return for all
concrete filters is always a List of type Item.  In the example shown, the id given “1001”, will reach
the data layer where a list will be populated with the type Item that matches the the Item with an
id of “1001”.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s