C++ 代理模式

在代理模式(Proxy Pattern)中,一个类代表另一个类的功能。这种类型的设计模式属于结构型模式。在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口。主要解决:在直接访问对象时带来的问题。何时使用:想在访问一个类时做一些控制。例如:我们有个游戏类,有初级玩家和中级玩家,还有代理玩家,初级玩家和中级玩家由于技术水平有限找了一个代理玩家帮自己通关打怪。

结构图:

UML图:

类实现代码Proxy.h:

/***************************************************************************** *C++ 设计模式之代理模式 *Copyright (C) 2020 @.com * *@file Proxy.h *@brief对文件的简述 *Details. * *@author Leeme *@[email protected] *@version1.0.0.1(版本号) *@date 2020.10.30 *@licenseGNU General Public License (GPL) * *---------------------------------------------------------------------------- *Remark : Description *---------------------------------------------------------------------------- *Change History : *<Date> | <Version> | <Author> | <Description> *---------------------------------------------------------------------------- *2020/10/30 | 1.0.0.1 | Leeme| Create file *---------------------------------------------------------------------------- * *****************************************************************************/ #pragma once #include <iostream> #include <string> /** * @brief 简单概述 * @brief 游戏类 */ class Game { public: Game(std::string name) { m_sGameName = name; } ~Game(){} std::string GetGameName() { return m_sGameName; } private: std::string m_sGameName; }; /** * @brief 简单概述 * @brief 玩家类 */ class Player { public: Player(Game* game){} virtual ~Player(){} virtual void OutputGameName() = 0; virtual void PlayGame() = 0; protected: Game* m_pGame; }; /** * @brief 简单概述 * @brief 代理玩家 */ class ProxyPlayer :public Player { public: ProxyPlayer(Game* game):Player(game) { m_pGame = game; } ~ProxyPlayer(){} void OutputGameName() { std::cout << "I am ProxyPlayer,Game name:" << m_pGame->GetGameName() << std::endl; } void PlayGame() { std::cout << "Start play game with ProxyPlayer." << std::endl; } }; /** * @brief 简单概述 * @brief 初级玩家 */ class PrimaryPlayer :public Player { public: PrimaryPlayer(Game* game) :Player(game) { m_pProxyPlayer = new ProxyPlayer(game); } ~PrimaryPlayer() { delete m_pProxyPlayer; m_pProxyPlayer = NULL; } void OutputGameName() { std::cout << "/-- PrimaryPlayer:OutputGameName --/" << std::endl; m_pProxyPlayer->OutputGameName(); } void PlayGame() { std::cout << "/-- PrimaryPlayer:PlayGame --/" << std::endl; m_pProxyPlayer->PlayGame(); } private: ProxyPlayer* m_pProxyPlayer; }; /** * @brief 简单概述 * @brief 中级玩家 */ class MediumPlayer :public Player { public: MediumPlayer(Game* game) :Player(game) { m_pProxyPlayer = new ProxyPlayer(game); } ~MediumPlayer() { delete m_pProxyPlayer; m_pProxyPlayer = NULL; } void OutputGameName() { std::cout << "/-- MediumPlayer:OutputGameName ---/" << std::endl; m_pProxyPlayer->OutputGameName(); } void PlayGame() { std::cout << "/-- MediumPlayer:PlayGame --/" << std::endl; m_pProxyPlayer->PlayGame(); } private: ProxyPlayer* m_pProxyPlayer; };

main.cpp:

/***************************************************************************** *C++ 设计模式之代理模式 *Copyright (C) 2020 @.com * *@file main.cpp *@brief对文件的简述 *Details. * *@author Leeme *@[email protected] *@version1.0.0.1(版本号) *@date 2020.10.30 *@licenseGNU General Public License (GPL) * *---------------------------------------------------------------------------- *Remark : Description *---------------------------------------------------------------------------- *Change History : *<Date> | <Version> | <Author> | <Description> *---------------------------------------------------------------------------- *2020/10/30 | 1.0.0.1 | Leeme| Create file *---------------------------------------------------------------------------- * *****************************************************************************/ #include "Proxy.h" int main(int argc, char* argv[]) { Game* mGame = new Game("刀塔"); ProxyPlayer* mProxyPlayer = new ProxyPlayer(mGame); mProxyPlayer->OutputGameName(); mProxyPlayer->PlayGame(); PrimaryPlayer* mRyPlayer = new PrimaryPlayer(mGame); mRyPlayer->OutputGameName(); mRyPlayer->PlayGame(); MediumPlayer* mMuPlayer = new MediumPlayer(mGame); mMuPlayer->OutputGameName(); mMuPlayer->PlayGame(); delete mGame; mGame = NULL; delete mProxyPlayer; mProxyPlayer = NULL; delete mRyPlayer; mRyPlayer = NULL; delete mMuPlayer; mMuPlayer = NULL; std::cout << std::endl; for (size_t i = 0; i < 1; i++) { std::cout << "* ** *** **** ***** ****** *******" << std::endl; std::cout << "** *** **** ***** ****** ******* *" << std::endl; std::cout << "*** **** ***** ****** ******* * **" << std::endl; std::cout << "**** ***** ****** ******* * ** ***" << std::endl; std::cout << "**** **** **** <==> **** **** ****" << std::endl; std::cout << "***** ****** ******* * ** *** ****" << std::endl; std::cout << "****** ******* * ** *** **** *****" << std::endl; std::cout << "******* * ** *** **** ***** ******" << std::endl; std::cout << "* ** *** **** ***** ****** *******" << std::endl; } std::cout << std::endl; std::cout << "<<<<<<<<== Hello! You are a good man! ==>>>>>>>" << std::endl; getchar(); return 0; }