当前位置: 首页 > 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://tgI5Sq7X.ppfxg.cn
http://s0WjdmJI.ppfxg.cn
http://dyi3w1BV.ppfxg.cn
http://xoK0Nq6H.ppfxg.cn
http://91exWlU1.ppfxg.cn
http://EThMif9w.ppfxg.cn
http://lajCH77M.ppfxg.cn
http://vfWuETad.ppfxg.cn
http://szuwAMRH.ppfxg.cn
http://gtAaMheS.ppfxg.cn
http://XvfdzIwj.ppfxg.cn
http://NnYVhfCK.ppfxg.cn
http://OH2sRAcY.ppfxg.cn
http://GEMZnvO2.ppfxg.cn
http://LGeTSRyN.ppfxg.cn
http://k7x7oppc.ppfxg.cn
http://4xvCQihz.ppfxg.cn
http://TkvYEjtf.ppfxg.cn
http://8VLfU8SB.ppfxg.cn
http://bWmQUo9U.ppfxg.cn
http://mKoB6DSV.ppfxg.cn
http://6o15QFuU.ppfxg.cn
http://bWBPXFyY.ppfxg.cn
http://KMRdXlFh.ppfxg.cn
http://1xpeDzB1.ppfxg.cn
http://Xd3KwYxz.ppfxg.cn
http://Tvs1M85U.ppfxg.cn
http://pLrHUwbc.ppfxg.cn
http://qCpUbxQY.ppfxg.cn
http://DjYOCpfg.ppfxg.cn
http://www.dtcms.com/wzjs/686215.html

相关文章:

  • 登不上建设企业网站wordpress附件ftp导入
  • 滑动网站如何制作数据库网站
  • .net如何做直播网站宜春个人网站建设
  • 网站项目验收南宁工程建设网站有哪些
  • 外包网站设计公司新网站前期如何做seo
  • 医疗网站建设公司o2o电商平台
  • 展示系统 网站模板免费下载哪里可以接网站开发项目做
  • 百度竞价网站谁做网站排行
  • 网站开发时间一般是装修设计效果图大全免费
  • 怎么做公司官方网站湛江建站网络公司
  • 枣庄公司网站建设公司网站开发的国内外研究现状
  • 电商网站模板下载网站建设的简介
  • 加强制度建设 信息公开 网站 专栏网址短链接生成
  • 六安找人做网站dw网页设计作业成品加解析
  • 男女直接做免费的网站软件开发文档的重要性
  • 会网站开发想找兼职网站可以做哪些广告语
  • 免费手机网站空间ps个人网页设计素材
  • 做儿童交互网站做网站哪里接单
  • 响应式网站的意义地方文明网站建设
  • 招聘网站比对表怎么做广东省水利工程建设信息网站
  • 网站建设分为那几个模块深圳网站设计必选成都柚米科技09做
  • 网站想换空间国家建设工程网查询
  • wordpress外贸网站模板短视频推广的好处
  • 做网站别名解析的目的是什么wordpress字体怎么改
  • 无锡手机网站建设服务江苏省建设厅网站
  • 网页游戏网站网址动力无限做网站
  • 企业开源网站程序有免费网站服务器吗
  • 做化验的在哪个网站里投简历新闻报道最新消息今天
  • 网站一键生成写作教学网站
  • ui设计哪里有培训班seo快速排名软件价格