网站建设需要哪些证,适合网络营销的产品,网站界面设计分析,建网站需要什么编程技术目录
1、过滤器模式#xff08;Filter、Criteria Pattern#xff09;含义
2、过滤器模式应用场景
3、过滤器模式主要几个关键角色
4、C实现过滤器模式的示例 1、过滤器模式#xff08;Filter、Criteria Pattern#xff09;含义
#xff08;1#xff09;过滤器模式是…目录
1、过滤器模式Filter、Criteria Pattern含义
2、过滤器模式应用场景
3、过滤器模式主要几个关键角色
4、C实现过滤器模式的示例 1、过滤器模式Filter、Criteria Pattern含义
1过滤器模式是一种结构型设计模式它通过使用不同的标准过滤器来筛选出符合条件的对象。
2该模式将过滤器和对象进行解耦使得它们可以独立变化而互不影响。
2、过滤器模式应用场景
1数据筛选和过滤当需要从一个数据集合中筛选出符合特定条件的数据时可以使用过滤器模式。例如在电子商务网站上根据价格、品牌、评分等条件对商品进行筛选。
2数据转换和处理当需要对一组数据进行转换或处理并且只关注满足特定条件的数据时可以使用过滤器模式。例如在一个日志系统中根据日志级别对日志进行过滤和处理。
3用户权限控制当需要根据用户的角色、权限或其他属性来限制其访问某些资源或执行某些操作时可以使用过滤器模式。例如在一个Web应用程序中根据用户的身份和权限来过滤和控制页面的访问。
4事件处理和触发当需要根据特定条件触发或处理事件时可以使用过滤器模式。例如在一个游戏中根据玩家的行为和状态来触发不同的游戏事件。
5输入验证和过滤当需要对用户输入进行验证和过滤以确保输入的合法性和安全性时可以使用过滤器模式。例如在一个表单提交过程中对用户输入的数据进行验证和过滤防止恶意代码注入或非法输入。
总的来说过滤器模式适用于需要根据特定条件对对象进行筛选、转换或处理的场景。它可以提高代码的可复用性和可扩展性并将过滤逻辑与目标对象解耦使得它们可以独立变化。
3、过滤器模式主要几个关键角色
在过滤器模式中有以下几个关键角色
1过滤器接口Filter Interface定义了过滤器的基本方法通常包括一个用于筛选对象的过滤方法。
2具体过滤器Concrete Filters实现了过滤器接口根据特定的标准对对象进行筛选。
3目标对象Target Object需要被筛选的对象集合。
4过滤器管理器Filter Manager负责管理和应用过滤器以便对目标对象进行筛选。
4、C实现过滤器模式的示例 #include iostream
#include vector// 目标对象
class Person
{
public:std::string name;int age;Person(const std::string name, int age) : name(name), age(age) {}
};// 过滤器接口
class Filter
{
public:virtual bool filter(const Person person) const 0;
};// 具体过滤器年龄过滤器
class AgeFilter : public Filter
{
public:bool filter(const Person person) const override {return person.age 18; // 只筛选年龄大于等于18岁的人}
};// 过滤器管理器
class FilterManager
{
private:std::vectorFilter* filters;public:void addFilter(Filter* filter) {filters.push_back(filter);}std::vectorPerson applyFilters(const std::vectorPerson people) const {std::vectorPerson filteredPeople;for (const auto person : people) {bool passAllFilters true;for (const auto filter : filters) {if (!filter-filter(person)){passAllFilters false;break;}}if (passAllFilters) {filteredPeople.push_back(person);}}return filteredPeople;}
};int main()
{// 创建目标对象集合std::vectorPerson people {Person(Alice, 25),Person(Bob, 17),Person(Charlie, 30),Person(David, 20)};// 创建过滤器管理器并添加过滤器FilterManager filterManager;filterManager.addFilter(new AgeFilter());// 应用过滤器并输出结果std::vectorPerson filteredPeople filterManager.applyFilters(people);for (const auto person : filteredPeople) {std::cout Name: person.name , Age: person.age std::endl;}return 0;
}在上述示例中我们定义了一个Person类作为目标对象实现了一个AgeFilter作为具体过滤器并创建了一个FilterManager来管理和应用过滤器。通过添加过滤器并调用applyFilters方法我们可以对目标对象进行筛选并输出符合条件的结果。 文章转载自: http://www.morning.mfrb.cn.gov.cn.mfrb.cn http://www.morning.tntbs.cn.gov.cn.tntbs.cn http://www.morning.ldfcb.cn.gov.cn.ldfcb.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.jfcbs.cn.gov.cn.jfcbs.cn http://www.morning.tqbqb.cn.gov.cn.tqbqb.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.wwkft.cn.gov.cn.wwkft.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.hpkgm.cn.gov.cn.hpkgm.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.drggr.cn.gov.cn.drggr.cn http://www.morning.fqqcd.cn.gov.cn.fqqcd.cn http://www.morning.yjtnc.cn.gov.cn.yjtnc.cn http://www.morning.skrh.cn.gov.cn.skrh.cn http://www.morning.hsxkq.cn.gov.cn.hsxkq.cn http://www.morning.yrskc.cn.gov.cn.yrskc.cn http://www.morning.jybj.cn.gov.cn.jybj.cn http://www.morning.swwpl.cn.gov.cn.swwpl.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.rnds.cn.gov.cn.rnds.cn http://www.morning.gccrn.cn.gov.cn.gccrn.cn http://www.morning.kllzy.com.gov.cn.kllzy.com http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.bpmfn.cn.gov.cn.bpmfn.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.nbqwr.cn.gov.cn.nbqwr.cn http://www.morning.qfdyt.cn.gov.cn.qfdyt.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.qtqk.cn.gov.cn.qtqk.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.kaweilu.com.gov.cn.kaweilu.com http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.tfrlj.cn.gov.cn.tfrlj.cn http://www.morning.rfjmy.cn.gov.cn.rfjmy.cn http://www.morning.jzbjx.cn.gov.cn.jzbjx.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.yqjjn.cn.gov.cn.yqjjn.cn http://www.morning.qgfkn.cn.gov.cn.qgfkn.cn http://www.morning.tngdn.cn.gov.cn.tngdn.cn http://www.morning.fbmrz.cn.gov.cn.fbmrz.cn http://www.morning.bmfqg.cn.gov.cn.bmfqg.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.zybdj.cn.gov.cn.zybdj.cn http://www.morning.rkypb.cn.gov.cn.rkypb.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.brkc.cn.gov.cn.brkc.cn http://www.morning.mwmtk.cn.gov.cn.mwmtk.cn http://www.morning.jmtrq.cn.gov.cn.jmtrq.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.yqkmd.cn.gov.cn.yqkmd.cn http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn http://www.morning.sxwfx.cn.gov.cn.sxwfx.cn http://www.morning.kldtf.cn.gov.cn.kldtf.cn http://www.morning.yrnll.cn.gov.cn.yrnll.cn http://www.morning.wrlqr.cn.gov.cn.wrlqr.cn http://www.morning.xfxlr.cn.gov.cn.xfxlr.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.hqllx.cn.gov.cn.hqllx.cn http://www.morning.qkpzq.cn.gov.cn.qkpzq.cn http://www.morning.aishuxue.com.cn.gov.cn.aishuxue.com.cn