当前位置: 首页 > news >正文

大型图片库网站建设深圳高端seo公司助力企业

大型图片库网站建设,深圳高端seo公司助力企业,网页设计与网站建设书,西安网站建设设计的好公司排名有一个广告召回系统,输入用户id就可以给用户推荐相应的广告,一开始我们只有布尔检索和向量检索两种方式。 1. 面向接口编程,而非实现 第一点就是定义接口,客户端关注的是接口,对客户端来说,他只关心检索引…

有一个广告召回系统,输入用户id就可以给用户推荐相应的广告,一开始我们只有布尔检索和向量检索两种方式。

1. 面向接口编程,而非实现

第一点就是定义接口,客户端关注的是接口,对客户端来说,他只关心检索引擎提供出来的接口是什么样子,不在意检索引擎是如何实现的。所以第一点就是抽象出接口。

class Recall {
public:virtual ~Recall() = default;virtual void recall(std::string uid) = 0;
};

2. 实现接口

我们有两个检索引擎,一个倒排,一个向量,根据接口分别实现这两种召回

// 倒排检索
class InvertRecall : public Recall {void recall(std::string uid) override {std::cout << uid << ": invert recall\n";}
};// 向量检索
class VectorRecall : public Recall {void recall(std::string uid) override {std::cout << uid << ": vector recall\n";}
};

3. 客户端调用

我们给客户端提供了两种召回方式,客户端其实并不关心的,他只关心接口是什么,通过这个接口他就可以拿到自己想要的结果

Recall* vectorRecall = new InvertRecall(); // 倒排召回
Recall* invertRecall = new VectorRecall(); // 向量召回
invertRecall->recall(uid);
vectorRecall->recall(uid);

我特意把名字写反了,就是为了表示客户端其实并不关心具体的召回方式是什么,谁来都一样,他只关心recall接口,根据recall接口就可以得到自己想要的结果,我们在改进一下

class AggRecall {
private:std::vector<std::shared_ptr<Recall>> m_recall;
public:void addRecall(const std::shared_ptr<Recall> &recall) {m_recall.emplace_back(recall);}void recall(std::string uid) {for (auto &recall: m_recall) {recall->recall(uid);}}
};

4. 新增了三方接口

除了我们自己实现的两种召回方式,还有一些其他的三方召回方式。当然,人家也很专业,也是面向接口编程,同时提供了一种实现方案。

// 三方代码勿动,以so方式提供
class ThirdPartyRecall {
public:virtual ~ThirdPartyRecall() = default;virtual void process(std::string uid, std::string thirdParty) = 0;
};class ThirdPartyRecallImp : public ThirdPartyRecall {
public:void process(std::string uid, std::string thirdParty) override {std::cout << thirdParty << " recall" << std::endl;}
};

这个时候客户端实现的AggRecall方案就要发生一些变化了,因此三方的接口名字和参数和我们实现的召回接口不一样,一种想当然的方式就是把所有召回引擎的接口统一,大家都叫recall,或者大家都叫process。

5. 统一接口

三方的召回接口是以so的方式提供的,我们想要修改代码也不现实了,一种愚蠢的方法就是修改自己的recall接口,但是你有没有想过,如果又有一个其他三方接口不一致怎么办呢?

// 三方代码勿动,以so方式提供
class GoogleRecall {
public:virtual ~GoogleRecall() = default;virtual void search(std::string uid) = 0;
};class GoogleRecallImp : public GoogleRecall {
public:void search(std::string uid) override {std::cout << uid << ": google search" << std::endl;}
};

6. 适配器

现在的问题就是如何把这些接口统一了,其实很简单,我们把三方的接口包一层不久可以了吗,或者说重命名不就可以了吗

class ThirdPartyAdapter : public Recall {
private:std::shared_ptr<ThirdPartyRecallImp> thirdPartyRecall;
public:explicit ThirdPartyAdapter(std::shared_ptr<ThirdPartyRecallImp> recall) : thirdPartyRecall(std::move(recall)) {}void recall(std::string uid) override {thirdPartyRecall->process(uid, "baidu");}
};

同样可以对其他的三方接口进行转换

class GoogleAdapter : public Recall {
private:std::shared_ptr<GoogleRecallImp> googleRecall;
public:explicit ThirdPartyAdapter(std::shared_ptr<GoogleRecallImp> recall) : googleRecall(std::move(recall)) {}void search(std::string uid) override {googleRecall->search(uid);}
};

就是这么简单,我们把process函数用一个recall函数包装起来,这样客户端在调用的时候依然调用的是recall函数,而且也继承了Recall接口

int main() {auto invertRecall = std::make_shared<InvertRecall>();auto vectorRecall = std::make_shared<VectorRecall>();auto thirdRecall = std::make_shared<ThirdPartyRecallImp>();auto googleRecall = std::make_shared<ThirdPartyAdapter>(thirdRecall);auto aggRecall = std::make_shared<AggRecall>();aggRecall->addRecall(invertRecall);aggRecall->addRecall(vectorRecall);aggRecall->addRecall(googleRecall);aggRecall->doRecall();
}

这个呢,就叫适配器,把其他接口转换成我们想要的接口。

http://www.tj-hxxt.cn/news/68020.html

相关文章:

  • 深圳建网站技术百度搜索推广登录入口
  • 龙岗网站建设深圳信科个人博客登录入口
  • 江苏网站推广公司免费发布推广的网站
  • 效果图制作公司哪家好seo关键字排名
  • 网站建设公司服务seo图片优化的方法
  • 什么网站做视频最赚钱视频号怎么推广流量
  • 域名解析后怎么做网站专业的网页制作公司
  • 西宁企业网站营销推广武汉关键词排名工具
  • 优秀网站建设报价国内十大搜索引擎网站
  • 公司申请网站需要哪些材料优化营商环境条例
  • 拼多多关键词推广seo网络推广机构
  • 每天4元代发广告重庆seo薪酬水平
  • 怎么让百度收录你的网站全网营销推广平台
  • 如何用front怕个做网站百度客服中心人工在线电话
  • 凡科网做的网站能直接用吗百度seo官方网站
  • 如何做网站拓扑结构图怎么做互联网营销推广
  • 摄影师个人网站制作百度网站关键词排名查询
  • 怎么提交网站收录郑州seo外包顾问
  • 网站 左右浮动 广告网站流量宝
  • 论坛类网站建设排名前十的小说
  • 传奇服务器如何做网站软文范例800字
  • 北京市办理居住卡在哪个网站做日本免费服务器ip地址
  • 国际电商平台有哪些百度搜索引擎优化详解
  • 制作网页框架的设计工具专业黑帽seo
  • 福建微网站建设广东seo
  • 聊城网站建设seo专业培训课程
  • 花生壳做网站缺点查域名ip地址查询
  • 网站制作公司优势seo做什么网站赚钱
  • 如何做图片 网站 链接seo是搜索引擎优化
  • 公司网站建设一条龙谷歌网页版登录入口