设计模式(13)-代理模式

来源:互联网 发布:好用又不贵的面霜知乎 编辑:程序博客网 时间:2024/06/10 01:26

代理模式

  代理模式属于结构型的设计模式,其特点是为其他对象提供一种代理以控制对这个对象的访问。
  代理模式有如下4种类型:
  1、远程代理,也就是为一个对象在不同的地址空间提供局部代表。这样可以隐藏一个对象存在于不同地址空间的事实。
  2、虚拟代理,是根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象。
  3、安全代理,用来控制真实对象访问的权限。
  4、智能指引,取代了简单的指针,它在访问对象时执行一些附加操作。

代码

  此处以安全代理来说明代理模式的设计,以游戏中不同VIP级别的权限区别为例子进行说明。

C++代码

  文件结构:
  —include
  ——Play.h
  ——Player.h
  ——ProxyPlayerVip1.h
  ——ProxyPlayerVip2.h
  —src
  ——Play.cpp
  ——Player.cpp
  ——ProxyPlayerVip1.cpp
  ——ProxyPlayerVip2.cpp
  ——main.cpp
  代码如下:
  —include/Play.h

#ifndef _PLAY_H_#define _PLAY_H_class Play{public:    virtual ~Play();    virtual void Play1()=0;    virtual void Play2()=0;    virtual void Play3()=0;};#endif

  —include/Player.h

#ifndef _PLAYER_H_#define _PLAYER_H_#include"Play.h"class Player:public Play{public:    virtual void Play1();    virtual void Play2();    virtual void Play3();};#endif

  —include/ProxyPlayerVip1.h

#ifndef _PROXYPLAYERVIP1_H_#define _PROXYPLAYERVIP1_H_#include"Play.h"#include<memory>class ProxyPlayerVip1:public Play{public:    ProxyPlayerVip1();    virtual void Play1();    virtual void Play2();    virtual void Play3();private:    std::shared_ptr<Play> player;};#endif

  —include/ProxyPlayerVip2.h

#ifndef _PROXYPLAYERVIP2_H_#define _PROXYPLAYERVIP2_H_#include"Play.h"#include<memory>class ProxyPlayerVip2:public Play{public:    ProxyPlayerVip2();    virtual void Play1();    virtual void Play2();    virtual void Play3();private:    std::shared_ptr<Play> player;   };#endif

  —src/Play.cpp

#include"Play.h"Play::~Play(){    ;}

  —src/Player.cpp

#include"Player.h"#include<iostream>using namespace std;void Player::Play1(){    cout<<"战役"<<endl;}void Player::Play2(){    cout<<"军团"<<endl;}void Player::Play3(){    cout<<"神器"<<endl;}

  —src/ProxyPlayerVip1.cpp

#include"ProxyPlayerVip1.h"#include"Player.h"#include<iostream>using namespace std;ProxyPlayerVip1::ProxyPlayerVip1():player(make_shared<Player>()){    ;}void ProxyPlayerVip1::Play1(){    player->Play1();}void ProxyPlayerVip1::Play2(){    player->Play2();}void ProxyPlayerVip1::Play3(){    cout<<"没有权限"<<endl;}

  —src/ProxyPlayerVip2.cpp

#include"ProxyPlayerVip2.h"#include"Player.h"#include<iostream>using namespace std;ProxyPlayerVip2::ProxyPlayerVip2():player(make_shared<Player>()){    ;}void ProxyPlayerVip2::Play1(){    player->Play1();}void ProxyPlayerVip2::Play2(){    player->Play2();}void ProxyPlayerVip2::Play3(){    player->Play3();}

  —src/main.cpp

#include<iostream>#include"ProxyPlayerVip1.h"#include"ProxyPlayerVip2.h"#include"Play.h"#include<memory>using namespace std;int main(){    shared_ptr<Play> player1 = make_shared<ProxyPlayerVip1>();    shared_ptr<Play> player2 = make_shared<ProxyPlayerVip2>();    player1->Play1();    player1->Play2();    player1->Play3();    player2->Play1();    player2->Play2();    player2->Play3();    return 0;}

Python代码

  文件结构:
  —Play.py
  代码如下:
  —Play.py

# -*- coding:utf-8 -*-class Play:    def Play1(self):        pass    def Play2(self):        pass    def Play3(self):        passclass Player(Play):    def Play1(self):        print "战役"    def Play2(self):        print "军团"    def Play3(self):        print "神器"class ProxyPlayerVip1(Play):    def __init__(self):        self.__player = Player()    def Play1(self):        self.__player.Play1()    def Play2(self):        self.__player.Play2()    def Play3(self):        print "没有权限"class ProxyPlayerVip2(Play):    def __init__(self):        self.__player = Player()    def Play1(self):        self.__player.Play1()    def Play2(self):        self.__player.Play2()    def Play3(self):        self.__player.Play3()if "__main__" == __name__:    player1 = ProxyPlayerVip1()    player2 = ProxyPlayerVip2()    player1.Play1()    player1.Play2()    player1.Play3()    player2.Play1()    player2.Play2()    player2.Play3()
0 0