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

网站注销申请宿迁网站建设要多少钱

网站注销申请,宿迁网站建设要多少钱,网站做支付需要准备什么,网站页面设计公司电话👨‍🎓 模式名称:代理模式(Proxy Pattern) 🧵 故事背景 小明的“校园万能生活平台”App上线后,大受欢迎! 然而没几天,他发现服务器响应变慢,数据库压力暴涨…

👨‍🎓 模式名称:代理模式(Proxy Pattern)

🧵 故事背景

小明的“校园万能生活平台”App上线后,大受欢迎!

然而没几天,他发现服务器响应变慢,数据库压力暴涨。查日志发现——

👾 有一群爬虫在疯狂请求接口!

GET /api/user_info?id=12345
GET /api/class_schedule
GET /api/favoritelist
...

小明都快疯了 😵‍💫!

于是,他请来了“小明安全屋团队”,做了两件事:

  1. 所有App API请求,都必须经过代理访问后端服务
  2. 在代理中加入身份验证、访问计数、缓存机制等功能

于是,“代理模式”就派上用场了!

🏗 没有代理模式(❌ 不加控制,裸奔服务器)

class RealServerAPI {
public:void getUserInfo(const std::string& userId) {std::cout << "📡 获取用户信息:" << userId << std::endl;}
};

客户端直接调用:

RealServerAPI api;
api.getUserInfo("xiaoming123");

🔥 没有身份校验、没有日志、不能限速,爬虫来了你就完蛋!

不使用代理模式做限制的完整代码

#include <iostream>
#include <string>#include <unordered_map>
#include <unordered_set>class UserService {
public:std::string getUserInfo(const std::string& userId) {// 权限校验if (blacklist.find(userId) != blacklist.end()) {return "⛔ 你被禁止访问";}// 访问计数accessCount[userId]++;std::cout << "👀 用户 " << userId << " 已访问 " << accessCount[userId] << " 次\n";// 缓存逻辑if (cache.find(userId) != cache.end()) {std::cout << "📦 从缓存中返回用户信息\n";return cache[userId];}// 模拟数据库查询std::string info = "📡 用户信息:" + userId;cache[userId] = info;return info;}private:std::unordered_set<std::string> blacklist = { "hacker" };std::unordered_map<std::string, int> accessCount;std::unordered_map<std::string, std::string> cache;
};int main() {UserService* realService = new UserService();// 客户端只面对 protectedServicestd::cout << realService->getUserInfo("xiaoming") << "\n\n";std::cout << realService->getUserInfo("xiaoming") << "\n\n";std::cout << realService->getUserInfo("hacker") << "\n\n";delete realService;
}

在这里插入图片描述

这里虽然做了限制,但是有如下缺点
😵‍💫 缺点:

  • 职责太多:业务类同时负责权限校验、计数、缓存、数据处理,难以维护。
  • 重复性强:每个接口都要复制这套逻辑。
  • 可测试性差:单测很困难,因为功能耦合严重。
  • 另外,有的时候类可能是第三方封闭库的一部分。这样我们就没办法做到上面的限制操作,因为我们没办法改别人的类

✅ 使用代理模式:增加身份验证 + 访问计数+ 缓存机制

1️⃣ 接口抽象:
class IUserService {
public:virtual std::string getUserInfo(const std::string& userId) = 0;virtual ~IUserService() = default;
};
2️⃣ 真实服务类:
class RealUserService : public IUserService {
public:std::string getUserInfo(const std::string& userId) override {return "📡 用户信息:" + userId;}
};
3️⃣ 权限代理:
class PermissionProxy : public IUserService {
public:PermissionProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {if (blacklist.find(userId) != blacklist.end()) {return "⛔ 你被禁止访问";}return service->getUserInfo(userId);}private:IUserService* service;std::unordered_set<std::string> blacklist = { "hacker" };
};
4️⃣ 计数代理:
class AccessCounterProxy : public IUserService {
public:AccessCounterProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {accessCount[userId]++;std::cout << "👀 用户 " << userId << " 已访问 " << accessCount[userId] << " 次\n";return service->getUserInfo(userId);}private:IUserService* service;std::unordered_map<std::string, int> accessCount;
};
5️⃣ 缓存代理:
class CacheProxy : public IUserService {
public:CacheProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {if (cache.find(userId) != cache.end()) {std::cout << "📦 从缓存中返回\n";return cache[userId];}std::string info = service->getUserInfo(userId);cache[userId] = info;return info;}private:IUserService* service;std::unordered_map<std::string, std::string> cache;
};
🏗 最终组装(链式代理):
int main() {IUserService* realService = new RealUserService();IUserService* cached = new CacheProxy(realService);IUserService* counted = new AccessCounterProxy(cached);IUserService* protectedService = new PermissionProxy(counted);// 客户端只面对 protectedServicestd::cout << protectedService->getUserInfo("xiaoming") << "\n\n";std::cout << protectedService->getUserInfo("xiaoming") << "\n\n";std::cout << protectedService->getUserInfo("hacker") << "\n";delete protectedService;delete counted;delete cached;delete realService;
}
完整代码
#include <iostream>
#include <string>#include <unordered_map>
#include <unordered_set>class IUserService {
public:virtual std::string getUserInfo(const std::string& userId) = 0;virtual ~IUserService() = default;
};class RealUserService : public IUserService {
public:std::string getUserInfo(const std::string& userId) override {return "📡 用户信息:" + userId;}
};class PermissionProxy : public IUserService {
public:PermissionProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {if (blacklist.find(userId) != blacklist.end()) {return "⛔ 你被禁止访问";}return service->getUserInfo(userId);}private:IUserService* service;std::unordered_set<std::string> blacklist = { "hacker" };
};class AccessCounterProxy : public IUserService {
public:AccessCounterProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {accessCount[userId]++;std::cout << "👀 用户 " << userId << " 已访问 " << accessCount[userId] << " 次\n";return service->getUserInfo(userId);}private:IUserService* service;std::unordered_map<std::string, int> accessCount;
};class CacheProxy : public IUserService {
public:CacheProxy(IUserService* service) : service(service) {}std::string getUserInfo(const std::string& userId) override {if (cache.find(userId) != cache.end()) {std::cout << "📦 从缓存中返回\n";return cache[userId];}std::string info = service->getUserInfo(userId);cache[userId] = info;return info;}private:IUserService* service;std::unordered_map<std::string, std::string> cache;
};int main() {IUserService* realService = new RealUserService();IUserService* cached = new CacheProxy(realService);IUserService* counted = new AccessCounterProxy(cached);IUserService* protectedService = new PermissionProxy(counted);// 客户端只面对 protectedServicestd::cout << protectedService->getUserInfo("xiaoming") << "\n\n";std::cout << protectedService->getUserInfo("xiaoming") << "\n\n";std::cout << protectedService->getUserInfo("hacker") << "\n";delete protectedService;delete counted;delete cached;delete realService;
}

在这里插入图片描述

✅ 优势总结

比较维度硬编码处理代理模式处理
结构清晰❌ 混乱✅ 职责分明
易于扩展❌ 修改原类代码✅ 添加新代理类即可
测试性好❌ 很难隔离测试✅ 每个代理功能可独立测试
灵活组装❌ 所有逻辑耦合✅ 可按需组合代理链
避免重复代码❌ 每个接口都要写一遍✅ 写一次代理,可全局复用

文章转载自:

http://GTxkLsQV.snccL.cn
http://2W3WKBQg.snccL.cn
http://aWdmv9UO.snccL.cn
http://zHPsdoBF.snccL.cn
http://q2xxbke3.snccL.cn
http://XJGfeBwv.snccL.cn
http://gPB84jaP.snccL.cn
http://lZDiMM5p.snccL.cn
http://J8aHgryy.snccL.cn
http://qE2b08VT.snccL.cn
http://UioLnl84.snccL.cn
http://YKW7IkRL.snccL.cn
http://oPyvVbnE.snccL.cn
http://Axe7AQdt.snccL.cn
http://idqE9ju7.snccL.cn
http://IRKAbtUX.snccL.cn
http://1MCpU4ga.snccL.cn
http://a6YhnENz.snccL.cn
http://CkSgnWSN.snccL.cn
http://F3b6rsnw.snccL.cn
http://c9pyAc6D.snccL.cn
http://UzyLZHsU.snccL.cn
http://YvbT6Jdm.snccL.cn
http://q9qwxJwV.snccL.cn
http://ipgV0NjL.snccL.cn
http://00t0kzTO.snccL.cn
http://K57PNTzd.snccL.cn
http://6onFwWwt.snccL.cn
http://fy23OJAg.snccL.cn
http://k7pnVlox.snccL.cn
http://www.dtcms.com/wzjs/644063.html

相关文章:

  • 广州市南沙区建设局网站公司企业网站开发
  • 柞水县住房和城乡建设局网站wordpress 订阅邮箱下载
  • 手机制作网站的软件有哪些微信同步wordpress
  • 维护网站是什么意思广西建设网怎么查询证件
  • 专门更新最新设计的网站百姓网全国免费发布信息
  • 凡科网站模板下载南京华夏商务网做网站怎么样
  • 护理学院网站建设让别人做网站是要每年续费吗
  • 广州网站建设服务商投资公司怎么运作
  • 自己做报名网站教程广告网架
  • 制作平台网站方案兼职做彩平网站
  • 建设母婴网站的总结做网站资讯
  • 软件园专业做网站东莞企业网站推广多少钱
  • 免费在线自助建站山东又一地检测阳性
  • 网站建设的前期准备wordpress 文章的标签
  • 四川城乡建设厅网站做网站该读啥学校
  • 济南专业的设计网站上海网站建设公司招聘
  • 小学生课程同步做网站软件网站开发留言板代码
  • 空包网站建设陇南比亚网站建设
  • 个人企业邮箱怎么申请网站seo优化推广怎么做
  • 郑州上街区网站建设公司品牌展示型网站源码
  • 临沂集团网站建设南宁seo标准
  • 网站根目录 设置新开传奇网站999
  • 怎么做货物收发的网站网页小游戏斗地主
  • 手机建站平台微点手赚网站建设运营知乎
  • 太原便宜做网站的公司为该网站做自适应
  • 一个公司可以做几个网站网站开通微信支付收费
  • 做网站需要学会些什么html教程 it教程网
  • 可以做片头的网站wordpress 代码详解
  • 浙江省城乡建设网站证件查询媒体吧软文平台
  • 服务器网站模板网站建设教程流程图