个人网站 创意,泉州网站建设报价,云做网站,网站技术招标怎么做概念
代理模式是一种结构型模式#xff0c;主要用于在客户端和接口之间添加一个中间层#xff0c;用于在客户端和接口之间进行权限控制或者其他的中间层操作。
使用场景
1、延缓初始化#xff0c;当我们偶尔需要使用一个重量级的服务对象#xff0c;如果一直保持该对象的…概念
代理模式是一种结构型模式主要用于在客户端和接口之间添加一个中间层用于在客户端和接口之间进行权限控制或者其他的中间层操作。
使用场景
1、延缓初始化当我们偶尔需要使用一个重量级的服务对象如果一直保持该对象的运行会消耗大量的系统资源这时候就可以使用代理。也可以控制重量级的对象进行即可销毁。
2、权限访问控制当我们希望特定的客户端使用接口的时候就可以使用代理模式。当复合我们的条件的时候再进行真正的接口访问。
3、本地执行远程服务远程代理这个时候使用代理进行远程服务的复杂处理。并且可以缓存请求结果。
4、记录日志当需要在接口和客户端之前进行日志记录的时候可以使用代理模式。
创建方式
1、如果有现成的服务器接口我们可以直接继承服务器接口实现代理类如果没有现成的服务接口我们就需要创建一个接口来实现服务对象和代理的可交换性。。
2、创建代理类其中必须包含一个指向服务接口的引用或者成员变量。一般情况下代理完全管理服务接口的生命周期。
3、根据需求实现代理接口。
类关系结构 示例代码
#include iostream
#include DaiLiMoShi.hint main()
{std::cout 欢迎东哥来到设计模式的世界!\n;Proxy proxy;proxy.setSerivePate(193.101.10.9, 2493);int post proxy.getSerivePost();cout main打印获取服务post数据 endl;cout Post : post endl;
}#pragma once
#include string
#include iostream
using namespace std;
class BaseProxyInterFaxe
{
public:BaseProxyInterFaxe() {}~BaseProxyInterFaxe() {}virtual int getSerivePost();virtual void setSerivePate(string ip, int post);
};class ThirdSerice : public BaseProxyInterFaxe {
public:ThirdSerice() {}~ThirdSerice() {}int getSerivePost() override;void setSerivePate(string ip, int post) override;
private:string m_ip;int m_post;
};class Proxy : public BaseProxyInterFaxe {
public:Proxy() {}~Proxy() {}int getSerivePost() override;void setSerivePate(string ip, int post) override;
private:ThirdSerice _thirdSerice;
};#include DaiLiMoShi.hint BaseProxyInterFaxe::getSerivePost()
{return 0;
}void BaseProxyInterFaxe::setSerivePate(string ip, int post)
{ //基类操作
}int Proxy::getSerivePost()
{//此处可以添加数据打印、权限判断等cout 代理打印获取服务post数据 endl;cout Post : _thirdSerice.getSerivePost() endl;return _thirdSerice.getSerivePost();
}void Proxy::setSerivePate(string ip, int post)
{//此处可以添加数据打印、权限判断等cout 代理设置服务位置 endl;cout IP : ip endl;cout Post : post endl;_thirdSerice.setSerivePate(ip, post);
}int ThirdSerice::getSerivePost()
{return m_post;
}void ThirdSerice::setSerivePate(string ip, int post)
{m_ip ip;m_post post;
}