设计模式之二-Proxy模式

代理模式

  组成:

  抽象角色:通过接口或抽象类声明真实角色实现的业务方法。
  代理角色:实现抽象角色,是真实角色的代理,通过真实角色的业务逻辑方法来实现抽象方法,并可以附加自己的操作。
  真实角色:实现抽象角色,定义真实角色所要实现的业务逻辑,供代理角色调用。
 
  代理模式的定义:为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
 
优点:
 
  (1).职责清晰
  真实的角色就是实现实际的业务逻辑,不用关心其他非本职责的事务,通过后期的代理完成一件完成事务,附带的结果就是编程简洁清晰。
  (2).代理对象可以在客户端和目标对象之间起到中介的作用,这样起到了中介的作用和保护了目标对象的作用。
  (3).高扩展性
 
模式结构:
  
  一个是真正的你要访问的对象(目标类),一个是代理对象,真正对象与代理
  对象实现同一个接口,先访问代理类再访问真正要访问的对象。
  代理模式分为静态代理、动态代理。
  静态代理是由程序员创建或工具生成代理类的源码,再编译代理类。所谓静态也就是在程序运行前就已经存在代理类的字节码文件,代理类和委托类的关系在运行前就确定了。
  动态代理是在实现阶段不用关心代理类,而在运行阶段才指定哪一个对象。
 
代理模式可以很好的将前后端分开,实现了松散耦合。代理模式属于结构型模式
 
抽象角色:Building.h
#ifndef BUILDING_H_
#define BUILDING_H_

#include <stdio.h>

class Building {
public:
    Building();
    virtual ~Building();

    virtual void soldHouse()=0;
    virtual void rentHouse()=0;
};

#endif /* BUILDING_H_ *
代理角色:Agency.h Agency.cpp
#ifndef AGENCY_H_
#define AGENCY_H_

#include "Building.h"

class Agency : public Building {
public:
    Agency(Building *pBuilding);
    ~Agency();

    void soldHouse();
    void rentHouse();

private:
    Building *m_Building;
};

#endif /* AGENCY_H_ */
#include "Agency.h"

Agency::Agency(Building *pBuliding) {
    // TODO Auto-generated constructor stub
    m_Building = pBuliding;
}

Agency::~Agency() {
    // TODO Auto-generated destructor stub
}


void Agency::soldHouse()
{
    m_Building->soldHouse();
}

void Agency::rentHouse()
{
    m_Building->rentHouse();
}

 真实角色:CommunityResident.h CommunityResident.cpp SmallProperty.h SmallProperty.cpp

#ifndef COMMUNITYRESIDENT_H_
#define COMMUNITYRESIDENT_H_

#include "Building.h"

class CommunityResident : public Building {
public:
    CommunityResident();
    ~CommunityResident();

    void soldHouse();
    void rentHouse();
};

#endif /* COMMUNITYRESIDENT_H_ */
#include "CommunityResident.h"

CommunityResident::CommunityResident() {
    // TODO Auto-generated constructor stub

}

CommunityResident::~CommunityResident() {
    // TODO Auto-generated destructor stub
}

void CommunityResident::soldHouse()
{
    printf("community resident sold house
");
}

void CommunityResident::rentHouse()
{
    printf("community resident rent house
");
}
#ifndef SMALLPROPERTY_H_
#define SMALLPROPERTY_H_

#include "Building.h"

class SmallProperty : public Building {
public:
    SmallProperty();
    ~SmallProperty();

    void soldHouse();
    void rentHouse();
};

#endif /* SMALLPROPERTY_H_ */
#include "SmallProperty.h"

SmallProperty::SmallProperty() {
    // TODO Auto-generated constructor stub

}

SmallProperty::~SmallProperty() {
    // TODO Auto-generated destructor stub
}

void SmallProperty::soldHouse()
{
    printf("small property sold house
");
}

void SmallProperty::rentHouse()
{
    printf("small property rent house
");
}

接口调用:main.cpp

#include "Agency.h"
#include "CommunityResident.h"
#include "SmallProperty.h"

void dealWithCommunity()
{
    Agency *pAgency;

    pAgency = new Agency(new CommunityResident());
    pAgency->soldHouse();
    pAgency->rentHouse();

    delete pAgency;
}

void dealWithSmallProperty()
{
    Agency *pAgency;

    pAgency = new Agency(new SmallProperty());
    pAgency->soldHouse();
    pAgency->rentHouse();

    delete pAgency;
}

int main(int argc,char **argv)
{
    dealWithCommunity();
    dealWithSmallProperty();
    return 0;
}
原文地址:https://www.cnblogs.com/wanzaiyimeng/p/6829769.html