一个简单的例子讲讲Android开发中的标准模式(过滤器模式)

标准模式(过滤器模式)可以根据设定的标准过滤对象。

现在我们来举个例子:一个生鲜App,我们可以根据商品的产地、以及是否为蔬菜进行过滤。

①创建过滤接口。

/**
 * 过滤接口
 */
interface Filter {
    //过滤准则
    fun meetCriteria(ingredients:List<Ingredient>):MutableList<Ingredient>
}

②创建商品原料类。

/**
 * 原料类
 * @param name:原料名称
 * @param local:产地
 * @param vegetarian:是否为蔬菜
 */
data class Ingredient(val name:String, val local:String, val vegetarian:Boolean){
}

③创建过滤器,这里我们分别创建“满足蔬菜标准的过滤器”、“满足本地产品的过滤器”、“满足非本地产品的过滤器”、“满足AND标准的过滤器”以及“满足OR标准的过滤器”,如下所示:

//满足蔬菜标准的过滤器
class VegetarianFilter:Filter {
    override fun meetCriteria(ingredients: List<Ingredient>): MutableList<Ingredient> {
        val vegetarianList = ArrayList<Ingredient>()
        for (ingredient in ingredients){
            if (ingredient.vegetarian){
                vegetarianList.add(ingredient)
            }
        }
        return vegetarianList
    }
}

//本地产品过滤器
class LocalFilter :Filter{
    override fun meetCriteria(ingredients: List<Ingredient>): MutableList<Ingredient> {
        val localList = ArrayList<Ingredient>()
        for (ingredient in ingredients){
            if (Objects.equals(ingredient.local,"辽宁")){
                localList.add(ingredient)
            }
        }
        return localList
    }
}

//非本地产品过滤器
class NotLocalFilter :Filter{
    override fun meetCriteria(ingredients: List<Ingredient>): MutableList<Ingredient> {
        val nonLocalList = ArrayList<Ingredient>()
        for (ingredient in ingredients){
            if (ingredient.local!="辽宁"){
                nonLocalList.add(ingredient)
            }
        }
        return nonLocalList
    }
}

//AND标准过滤器
class AndCriteria(private val criteria:Filter, private val otherCriteria:Filter) :Filter{
    override fun meetCriteria(ingredients: List<Ingredient>): MutableList<Ingredient> {
        val firstCriteriaList  = criteria.meetCriteria(ingredients)
        return otherCriteria.meetCriteria(firstCriteriaList)
    }
}

//OR标准过滤器
class OrCriteria(private val criteria:Filter, private val otherCriteria:Filter) :Filter{
    override fun meetCriteria(ingredients: List<Ingredient>): MutableList<Ingredient> {
        val firstCriteriaList  = criteria.meetCriteria(ingredients)
        val nextCriteriaList = otherCriteria.meetCriteria(ingredients)
        for (ingredient in nextCriteriaList){
            if (!firstCriteriaList.contains(ingredient)){
                firstCriteriaList.add(ingredient)
            }
        }
        return firstCriteriaList
    }
}

④创建测试数据集、过滤器来进行测试。

//测试标准模式
//1.添加一个数据集
val ingredients = ArrayList<Ingredient>()
ingredients.add(Ingredient("黄瓜","辽宁",true))
ingredients.add(Ingredient("鸡肉","黑龙江",false))
ingredients.add(Ingredient("西红柿","山东",true))
ingredients.add(Ingredient("河蟹","辽宁",false))
//2.创建过滤器
val local = LocalFilter()
val notLocal = NotLocalFilter()
val vegetarian = VegetarianFilter()
val localAndVegetarian = AndCriteria(local,vegetarian)
val localOrVegetarian = OrCriteria(local,vegetarian)
//3.测试模式
Log.d(TAG,"本地:${local.meetCriteria(ingredients).toString()}")
Log.d(TAG,"非本地:${notLocal.meetCriteria(ingredients).toString()}")
Log.d(TAG,"蔬菜:${vegetarian.meetCriteria(ingredients).toString()}")
Log.d(TAG,"本地蔬菜:${localAndVegetarian.meetCriteria(ingredients).toString()}")
Log.d(TAG,"本地或者蔬菜:${localOrVegetarian.meetCriteria(ingredients).toString()}")

测试结果如下所示:
在这里插入图片描述
*本地:[Ingredient(name=黄瓜, local=辽宁, vegetarian=true), Ingredient(name=河蟹, local=辽宁, vegetarian=false)]

非本地:[Ingredient(name=鸡肉, local=黑龙江, vegetarian=false), Ingredient(name=西红柿, local=山东, vegetarian=true)]
蔬菜:[Ingredient(name=黄瓜, local=辽宁, vegetarian=true), Ingredient(name=西红柿, local=山东, vegetarian=true)]
本地蔬菜:[Ingredient(name=黄瓜, local=辽宁, vegetarian=true)]
本地或者蔬菜:[Ingredient(name=黄瓜, local=辽宁, vegetarian=true), Ingredient(name=河蟹, local=辽宁, vegetarian=false), Ingredient(name=西红柿, local=山东, vegetarian=true)]*

可以看到,仅仅是应用了一些简单的标准就可以很容易地使用合适的过滤器过滤相关符合选择条件的信息。

你可能感兴趣的:(Android开发,设计模式,android,kotlin)