C++ 代理模式(Proxy Pattern)

2023-09-17 14:24

随意写的,主要用作代码练习,防止代码生疏,敬请见谅!

本意是能够写成一个多用途的代理模式,结果还是写废了,其他方面来说,没有时间重整它,有的写法是没有太大意义的。期望有点价值吧

首先放一个代理模式的讲解,个人觉得写得不错,如果你是寻求代理模式的用法用途的话,值得一看:

代理模式原理及实例讲解

//ProxyPattern.h

#pragma once
#include 
#include 
#include 
template
class IMutiUser
{
public:virtual	ReturnValue (T)(...)= 0;virtual ReturnValue GetReturnType()const = 0;
};
template
class IMutiProxy :public IMutiUser
{
protected:T2*  mMImpl;
public:virtual ReturnValue (T)(...) override;virtual ReturnValue GetReturnType()const override;
};
template
ReturnValue IMutiProxy::T(...)
{mMImpl = new T2();if (mMImpl != nullptr){std::cout << "Show Something of IMutiProxy!" << std::endl;mMImpl->T();}ReturnValue temp= NULL;return temp;
}
template
ReturnValue IMutiProxy::GetReturnType()const
{ReturnValue temp=NULL;return temp;
}
template
class IMutiImplement :public IMutiUser
{
public:virtual ReturnValue(T)(...) override;virtual ReturnValue GetReturnType() const override;
};
template
ReturnValue IMutiImplement::T(...)
{std::cout << "show something!" << std::endl;std::cout << "You could dispose something for custom function ." << std::endl;ReturnValue temp=NULL;return temp;
}
template
ReturnValue IMutiImplement::GetReturnType() const
{ReturnValue temp=NULL;return temp;
}
//ProxyPattern.cpp

#include "ProxyPattern.h"
int show(int number)
{std::cout << "Current Number is :" + number << std::endl;return 1;
}
int main(...)
{int(*func)(int b);func = show;IMutiUser>* temp = new IMutiProxy>();temp->T();temp->GetReturnType();system("pause");return 0;
}