Proxy mode of design mode (C++)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

1. What is the proxy model?

       Proxy mode is a structural software design mode that provides a proxy to control access to the original object without changing the original code.

       Advantages of proxy mode:

  1. Responsibilities are clear. Real objects focus on their own business logic, without considering other non-professional content, and leave it to the agent to complete.
  2. High scalability. Changes to the real object do not affect the proxy.
  3. Decoupling. Separate the client from the real object to reduce system coupling.
  4. Improve performance. Virtual agents can reduce the consumption of system resources.
  5. High security and stability. Proxy can well control access and improve program security.

      Disadvantages of proxy mode:

  1. Increase system complexity. The duties of an agent are often complex.
  2. Requests are slowed down. Adding proxies to clients and real objects will reduce the operating efficiency of the entire system process to a certain extent.

2. Proxy mode

2.1 Structure diagram

       The client is the Main function. In the example in this article, the agent adds an additional function - recharge, so the client directly calls the agent.

2.2 Code example

       Scenario description: Games often have their own agents. If you want to play World of Warcraft, you need to communicate with the agent. After recharging, you can play normally.

//Game.h
/****************************************************/
#pragma once
#include <iostream>

using namespace std;

#include <iostream>
using namespace std;

// 抽象游戏类
class Game 
{
public:
	// 游戏加载
	virtual void load() = 0;

	// 游戏退出
	virtual void exit() = 0;
};

// 真实游戏类-魔兽世界
class WOW : public Game 
{
public:
	// 游戏加载
	virtual void load() {
		cout << "魔兽世界加载。" << endl;
	}

	// 游戏退出
	virtual void exit() {
		cout << "魔兽世界退出。" << endl;
	}
};

// 代理类-魔兽代理
class ProxyWOW : public Game 
{
public:
	// 构造函数
	ProxyWOW() {
		m_wow = new WOW();
	}

	// 析构函数
	virtual ~ProxyWOW() {
		if (m_wow != nullptr) {
			delete m_wow;
			m_wow = nullptr;
		}
	}

	// 充值时间
	void recharge(int money) {
		m_time += money / 100;
		cout << "充值:" << money << endl;
		cout << "获得时长:" << m_time << endl;
	}

	// 游戏加载
	virtual void load() {
		cout << "代理启动。" << endl;
		if (m_time > 0) {
			m_wow->load();
			cout << "游戏时长1小时。" << endl;
			m_time -= 1;
			cout << "剩余时长:" << m_time << endl;
			flag = true;
		}
		else {
			cout << "剩余游戏时长不足,请充值。" << endl;
			flag = false;
		}
	}

	// 游戏退出
	virtual void exit() {
		if (flag) {
			m_wow->exit();
			flag = false;
		}
		cout << "代理关闭。" << endl;
	}

private:
	bool flag = false;
	int m_time = 0;
	WOW* m_wow;
};

//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Subject.h"

using namespace std;

int main()
{
	// 玩魔兽
	ProxyWOW* proxy = new ProxyWOW();
	// 加载
	proxy->load();
	// 充值
	proxy->recharge(1000);
	// 加载
	proxy->load();
	// 退出
	proxy->exit();
	// 清理内存
	delete proxy;
	proxy = nullptr;
	return 0;

}

       The program results are as follows.

       The example provided in this article is about games. Playing large-scale online games often requires an agent platform, so I simulated a simple recharge and game process. Agents can be seen everywhere in life, and various intermediary services are a kind of agent; agents are also very common in network applications, such as server agents and so on.

3. Summary

       I try my best to express my understanding of the proxy mode with more common words and intuitive code routines. There may be some places that are not thoughtful. If you have different opinions, welcome to communicate in the comment area! Hope my example will help you understand the proxy pattern better.

       If the article helps you, you can give me a like to let me know, I will be very happy ~ come on!

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/129949951