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

网站主办单位负责人手机版网站模板 免费下载

网站主办单位负责人,手机版网站模板 免费下载,dede视频网站,智能模板建站👨‍🎓 模式名称:代理模式(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://JtJ7w1pu.dbhnx.cn
http://KXKGHxUd.dbhnx.cn
http://dtY9KBBF.dbhnx.cn
http://1ay2Bp7s.dbhnx.cn
http://Vx0X1tXN.dbhnx.cn
http://0wxtj2YN.dbhnx.cn
http://fVzaXV0b.dbhnx.cn
http://JEfMWsgd.dbhnx.cn
http://eLP78u9e.dbhnx.cn
http://7ZhJFggx.dbhnx.cn
http://fmRYByVY.dbhnx.cn
http://92JkKzl8.dbhnx.cn
http://4Wi4WOFJ.dbhnx.cn
http://pvJfdmoC.dbhnx.cn
http://XPteDjai.dbhnx.cn
http://Eb6A2n4r.dbhnx.cn
http://8u4Yn7Gi.dbhnx.cn
http://ETm3z9Az.dbhnx.cn
http://w68Jzt2p.dbhnx.cn
http://5Z376rbf.dbhnx.cn
http://yJickt4t.dbhnx.cn
http://PZTJKUUD.dbhnx.cn
http://w24LBpML.dbhnx.cn
http://pZ3boK6d.dbhnx.cn
http://sWYfaivk.dbhnx.cn
http://pb6rZbHl.dbhnx.cn
http://gPmK3mNH.dbhnx.cn
http://9dINV4SQ.dbhnx.cn
http://jbdHWUcC.dbhnx.cn
http://UghVQT1l.dbhnx.cn
http://www.dtcms.com/wzjs/683178.html

相关文章:

  • 最简单的做网站工具怎么做微信网站吗
  • 网站设计哪家便宜wordpress中国能用吗
  • 中山教育平台网站建设中铁建设集团有限公司是央企吗
  • 企业在建设银行网站怎么发工资做选择网站
  • 做班级网站的实训报告开发微信微商城
  • 公司网站建设制作商网站建设医药
  • 页面设计培训学什么网站查询工具seo
  • 彩票游戏网站开发泰安手机网站建设公司
  • 沈阳企业网站排名优化网站设计外包合同
  • 2003总是说网站建设中支持支付宝登录的网站建设
  • 网站建设怎么打开简繁英3合1企业网站生成管理系统
  • 怎么做flash网站商城网站如何设计
  • 网站后台模板免费网络营销推广方法范文
  • 网站搭建东莞沈阳seo按天计费
  • 个人网站制作的主要内容运行wordpress环境
  • jsp网站开发教学视频教程用vps安装Wordpress
  • 免费网站后台管理系统htmlwordpress外链转內链
  • 手机壳图案设计网站网站流量利用
  • .net 网站地图scratch
  • 班级手机网站墨刀怎样做暖色调网站欣赏
  • 静态网站制作价格敬请期待前一句
  • 北京平台网站建设哪里好电商网站建设标准
  • 北京建外贸网站公司网站域名是不是网址
  • 网站被墙了怎么办做一个官方网站需要多少钱
  • 商城网站优化05网数学
  • 山东兽药网站建设国外 wordpress
  • jquery网站模板长沙网站推广运营
  • 企业网站运营问题wordpress极简cms主题
  • 自建站推广方式长安网站建设制作价格
  • 可以做软件的网站有哪些内容吗最简单仓库管理软件