其他分享
首页 > 其他分享> > 徒手撸设计模式-过滤器模式

徒手撸设计模式-过滤器模式

作者:互联网

概念

过滤器模式(Filter Pattern)或标准模式(Criteria Pattern)是一种设计模式,这种模式允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。

参考链接: https://www.runoob.com/design-pattern/filter-pattern.html

代码案例

公共类

public final class StFlag {
    public StFlag() {
    }

    /**
     * 汽车
     */
    public static final String CAR="CAR";
    /**
     * 自行车
     */
    public static final String BICYCLE="BICYCLE";
    /**
     * 蓝色
     */
    public static final String BLUE="BLUE";
    
    /**
     * 交通工具类型-宝马
     */
    public static final String TRANSPORT_TYPE_BMW="BMW";
    /**
     * 交通工具类型-奔驰
     */
    public static final String TRANSPORT_TYPE_BENZ="BENZ";
    /**
     * 交通工具类型-捷安特
     */
    public static final String TRANSPORT_TYPE_GIANT="GIANT";
    /**
     * 交通工具类型-美利达
     */
    public static final String TRANSPORT_TYPE_MERIDA="MERIDA";
}

 

写一个实体类,有三个属性

/**
 * 交通工具实体类
 */
@Getter
@AllArgsConstructor
@Setter
@ToString
public class Transport {
    private String name;
    private String type;
    private String colour;
}

 

写一个接口,用于过滤匹配的数据

public interface Match {
    public List<Transport> getMatches(List<Transport>  transports);
}

 

五个实现类

/**
 * 自行车匹配实现类
 */
public class BicycleMatch implements Match {
    @Override
    public List<Transport> getMatches(List<Transport> transports) {
        List<Transport> transportsBicycle = new ArrayList<>();
        for (Transport transport : transports) {
            if (transport.getType().equalsIgnoreCase(StFlag.BICYCLE)) {
                transportsBicycle.add(transport);
            }
        }
        return transportsBicycle;
    }
}

 

/**
 * 汽车匹配实现类
 */
public class CarMatch implements Match {
    @Override
    public List<Transport> getMatches(List<Transport> transports) {
        List<Transport> transportsCar = new ArrayList<>();
        for (Transport transport : transports) {
            if (transport.getType().equalsIgnoreCase(StFlag.CAR)) {
                transportsCar.add(transport);
            }
        }
        return transportsCar;
    }
}

 

/**
 * 颜色匹配实现类
 */
public class BlueMatch implements Match {
    @Override
    public List<Transport> getMatches(List<Transport> transports) {
        List<Transport> transportsBlue = new ArrayList<>();
        for (Transport transport : transports) {
            if (transport.getColour().equalsIgnoreCase(StFlag.BLUE)) {
                transportsBlue.add(transport);
            }
        }
        return transportsBlue;
    }
}

 

/**
 * 且运算
 */
public class AndMatch implements Match {
    private Match bicycleMatch;
    private Match blueMatch;

    public AndMatch(Match bicycleMatch, Match blueMatch) {
        this.bicycleMatch = bicycleMatch;
        this.blueMatch = blueMatch;
    }

    @Override
    public List<Transport> getMatches(List<Transport> transports) {
        List<Transport> cars = blueMatch.getMatches(transports);
        return bicycleMatch.getMatches(cars);
    }
}

 

/**
 * 或运算
 */
public class OrMatch implements Match {
    private Match bicycleMatch;
    private Match carMatch;

    public OrMatch(Match bicycleMatch, Match carMatch) {
        this.bicycleMatch = bicycleMatch;
        this.carMatch = carMatch;
    }

    @Override
    public List<Transport> getMatches(List<Transport> transports) {
        List<Transport> cars = carMatch.getMatches(transports);
        List<Transport> bicycles = bicycleMatch.getMatches(transports);
        for (Transport car : cars) {
            if (!bicycles.contains(car)){
                bicycles.add(car);
            }
        }
        return bicycles;
    }
}

 

测试主类

/**
 * 设计模式控制器
 */
@RestController
@RequestMapping("/designPattern")
@Slf4j
public class DesignController {

    @PostMapping("/filter")
    public ResponseModel filter(@RequestBody List<Transport> transports) {
        log.info("DesignController--- filter --start--");

        Match carMatch = new CarMatch();
        Match blueMatch = new BlueMatch();
        BicycleMatch bicycleMatch = new BicycleMatch();
        Match andMatch = new AndMatch(bicycleMatch,blueMatch);
        Match orMatch = new OrMatch(bicycleMatch, carMatch);
        List<Transport> carList = carMatch.getMatches(transports);
        List<Transport> blueList = blueMatch.getMatches(transports);
        List<Transport> bicycleList = bicycleMatch.getMatches(transports);
        List<Transport> andList = andMatch.getMatches(transports);
        List<Transport> orList = orMatch.getMatches(transports);
        Map<String, List> result= new HashMap<>();
        result.put("carList",carList);
        result.put("blueList",blueList);
        result.put("bicycleList",bicycleList);
        result.put("andList",andList);
        result.put("orList",orList);
        log.info("DesignController--- filter --end--");
        log.info("result is : {}", JSON.toJSONString(result,true));
        return new ResponseModel("过滤器模式完成", 200, result);
    }
}

 

测试案例

 

 控制台日志

2022-06-22 01:27:43.211 INFO  DesignController--- filter --start-- 【http-nio-8081-exec-2】【DesignController:60】
2022-06-22 01:27:43.214 INFO  DesignController--- filter --end-- 【http-nio-8081-exec-2】【DesignController:78】
2022-06-22 01:27:43.222 INFO  result is : {
    "andList":[
        {
            "colour":"blue",
            "name":"giant",
            "type":"bicycle"
        },
        {
            "colour":"blue",
            "name":"feige",
            "type":"bicycle"
        }
    ],
    "bicycleList":[
        {"$ref":"$.andList[0]"},
        {"$ref":"$.andList[1]"},
        {
            "colour":"write",
            "name":"fenghuang",
            "type":"bicycle"
        }
    ],
    "orList":[
        {"$ref":"$.andList[0]"},
        {"$ref":"$.andList[1]"},
        {"$ref":"$.bicycleList[2]"},
        {
            "colour":"blue",
            "name":"bmw",
            "type":"car"
        },
        {
            "colour":"yellow",
            "name":"benz",
            "type":"car"
        },
        {
            "colour":"blue",
            "name":"poche",
            "type":"car"
        }
    ],
    "blueList":[
        {"$ref":"$.orList[3]"},
        {"$ref":"$.andList[0]"},
        {"$ref":"$.andList[1]"},
        {"$ref":"$.orList[5]"}
    ],
    "carList":[
        {"$ref":"$.orList[3]"},
        {"$ref":"$.orList[4]"},
        {"$ref":"$.orList[5]"}
    ]
} 【http-nio-8081-exec-2】【DesignController:79】

 

 

标签:bicycleMatch,List,过滤器,getMatches,设计模式,transports,public,Match,徒手
来源: https://www.cnblogs.com/hikoukay/p/16399026.html