zoukankan      html  css  js  c++  java
  • C++设计模式之代理模式

    源码在VC++ 6.0通过编译

     1 #include "ProxyHeader.h"
     2 #include <string>
     3 #include <iostream>
     4 
     5 using namespace std;
     6 
     7 class IPlayer{
     8 public:
     9     virtual void Login(string name,string password) = 0;
    10     virtual void KillBoss() = 0;
    11     virtual void Upgrade() = 0;
    12 };
    13 
    14 class GamePlayer : public IPlayer
    15 {
    16 public:
    17     GamePlayer(string name,string password);
    18     virtual void Login(string name,string password);
    19     virtual void KillBoss();
    20     virtual void Upgrade();
    21 private:
    22     int grade;
    23     string name;
    24     string password;
    25 };
    26 
    27 class PlayerProxy : public IPlayer
    28 {
    29 public:
    30     IPlayer* iplayer;
    31     PlayerProxy(IPlayer* iplayer){
    32         this->iplayer = iplayer;
    33     }
    34     virtual void Login(string name,string password)
    35     {
    36         this->iplayer->Login(name,password);
    37     }
    38     virtual void KillBoss(){
    39         this->iplayer->KillBoss();
    40     }
    41     virtual void Upgrade(){
    42         this->iplayer->Upgrade();
    43     }    
    44 };
    View Code
     1 //ProxySrc.cpp
     2 #include "ProxyHeader.h"
     3 
     4 
     5 GamePlayer::GamePlayer(string name,string password):grade(0){
     6     this->name = name;
     7     this->password = password;
     8 }
     9 void GamePlayer::Login(string name,string password)
    10 {
    11     cout<<"登录名:"<<this->name<<",密码为:"<<this->password<<endl;
    12 }
    13 void GamePlayer::KillBoss(){
    14     cout<<this->name<<"正在杀BOSS"<<endl;
    15 }
    16 void GamePlayer::Upgrade(){
    17     ++grade;
    18     cout<<this->name<<"已经"<<grade<<"级了"<<endl;
    19     }
    20 
    21 int main(int argc, char **argv)
    22 {
    23     IPlayer* iplayer1 = new GamePlayer("Jerome","1986");
    24     IPlayer* iplayer2 = new GamePlayer("Jessica","1987");
    25     IPlayer* iplayerproxy1 = new PlayerProxy(iplayer1);
    26     IPlayer* iplayerproxy2 = new PlayerProxy(iplayer2);
    27 
    28     iplayerproxy1->Login("游戏代练1","2013");
    29     iplayerproxy1->KillBoss();
    30     for (int i=0;i<10;i++)
    31     {
    32         iplayerproxy1->Upgrade();
    33     }
    34 
    35     iplayerproxy2->Login("游戏代练2","2013");
    36     iplayerproxy2->KillBoss();
    37     for (i=0;i<10;i++)
    38     {
    39         iplayerproxy2->Upgrade();
    40     }
    41 
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    MFC中处理消息的几个函数之间的区别
    双缓冲技术2
    CxImage简单用法2
    C/C++中 const,extern,static,volatile的使用(转帖)
    用BoundsChecker检测内存泄露2
    用BoundsChecker检测内存泄漏
    TrackMouseEvent函数实现鼠标停留响应
    3D——VTK使用
    防止密码被非法获取
    未来界面设计的主流——WPF技术
  • 原文地址:https://www.cnblogs.com/jeromesunny/p/3224197.html
Copyright © 2011-2022 走看看