为类提供另一个代理类,控制对该类的访问。
代码示例:
class ICar
{
public:
virtual void driveCar() = 0;
};
class Car : public ICar
{
public:
virtual void driveCar() override
{
cout << "Car has been driver!" << endl;
}
};
class ProxyCar : public ICar
{
public:
int _driver_age;
ICar* _realCar;
public:
ProxyCar(int driver_age)
: _realCar(new Car())
, _driver_age(driver_age)
{
}
~ProxyCar()
{
delete _realCar;
}
virtual void driverCar() override
{
if(_driver_age <= 16)
{
cout << "sorry, the driver is too young to drive." << endl;
}
else
{
_realCar->driverCar();
}
}
};
void main()
{
ICar* car = new ProxyCar(16);
car->DriverCar();
delete car;
car = new ProxyCar(25);
car->DriverCar();
delete car;
}