C++设计模式-代理模式
生活随笔
收集整理的這篇文章主要介紹了
C++设计模式-代理模式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
?
?
基本概念
代碼及演示
?
基本概念
代理模式(Proxy),為其他對象提供一種代理以控制對這個對象的訪問。
UML圖如下:
以本人目前(2019-06-12)個人的水平對代理的認識:
真實對象可以用于存儲數(shù)據(jù),并做一些基本的邏輯。
代理可以進行復制的邏輯。
?
代碼及演示
程序運行截圖如下:
結(jié)構(gòu)如下:
Head.h
#ifndef HEAD_H #define HEAD_H#include <iostream> #include <string> using namespace std;class Subject{public:virtual void request(){} };class RealSubject: public Subject{public:void request();RealSubject();protected://某些數(shù)據(jù)int m_intValue;string m_strValue;float m_floatValue; };class ProxySubject: public Subject{public:void request();ProxySubject();private:RealSubject *m_realSubject; };#endif //HEAD_HHead.cpp
#include "Head.h"void RealSubject::request() {cout << "RealSubject request called!" << endl;cout << "m_inValue is : " << m_intValue << endl;cout << "m_strValue is : " << m_strValue << endl;cout << "m_floatValue is : " << m_floatValue << endl; }RealSubject::RealSubject() {cout << "RealSubject construction called!" << endl;m_intValue = 100;m_strValue = "Hello World!";m_floatValue = 100.01; }void ProxySubject::request() {cout << "ProxySubject request called!" << endl;m_realSubject->request(); }ProxySubject::ProxySubject() {cout << "ProxySubject construction called!" << endl;m_realSubject = new RealSubject;}main.cpp
#include "Head.h"int main(int argc[], int *argv[]){ProxySubject *proxySubject = new ProxySubject;proxySubject->request();delete proxySubject;getchar();return 0; }?
總結(jié)
以上是生活随笔為你收集整理的C++设计模式-代理模式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++笔记-利用远程线程注入获取PC版微
- 下一篇: Java笔记-java web实现验证码