O'Reilly logo

Android Design Patterns and Best Practice by Kyle Mew

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The criteria pattern

The criteria design pattern provides a clear and concise technique for filtering objects according to set criteria. It can be a very powerful tool as this next exercise will demonstrate.

In this example, we will apply a filter pattern to sort through a list of ingredients and filter them according to whether they are vegetarian and where they are produced:

  1. Begin by creating the filter interface, like so:
    public interface Filter { 
     
        List<Ingredient> meetCriteria(List<Ingredient> ingredients); 
    } 
    
  2. Next add the ingredient class, like this:
    public class Ingredient { String name; String local; boolean vegetarian; public Ingredient(String name, String local, boolean vegetarian){ this.name = name; this.local = local; this.vegetarian = vegetarian; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required