[c++]代理对象模式
来源:互联网 发布:linux中chown r 编辑:程序博客网 时间:2024/05/20 19:17
代理对象
#include <iostream>#include <vector>using namespace std;class Vehicle { public: Vehicle() {} virtual string getName() = 0;};class Car : public Vehicle { public: Car() { name = "car"; } virtual string getName() { return name; } ~Car() { cout << "delete" << endl; }private: string name;};class Bike : public Vehicle { public: Bike() {} virtual string getName() { return "bike"; }private: string name;};void wrong_code(vector<Vehicle *> &parkinglot) { Car c; cout << &c << endl; parkinglot[0] = &c; //将c插入到parkinglot内 cout << &(*parkinglot[0]) << endl;}int main() { vector<Vehicle *> parkinglot(1); wrong_code(parkinglot); cout << parkinglot[0]->getName() << endl; // Oops!!parkinglot内存的地址指向了一块被销毁的内存 cout << &(*parkinglot[0]) << endl; return 0;}
0x7fff5fbff6d8
0x7fff5fbff6d8
delete
0x7fff5fbff6d80x7fff5fbff6d8delete乱码。。。。0x7fff5fbff6d8
在这种情况下会出现因为临时对象在生命周期结束之后,对象上的内存被释放,所以访问它的数据就会出现不可预计的结果。
在这种大前提下,代理类就顺势而生了.
顾名思义,代理类就是指某个基类以及其子类的代理,其功能是使之能在容器中也表现出多态性.而没有动态内存管理的烦恼.
现在定义Vehicle以及子类的代理类VehicleSurrogate.
#include <iostream>#include <vector>using namespace std;class Vehicle { public: Vehicle() {} virtual string getName() = 0; virtual Vehicle *copy() const = 0; virtual ~Vehicle() {} //虚析构函数是为了支持多态.但是本例中并不需要. private:};class Car : public Vehicle { public: Car() { name = "car"; } virtual string getName() { return name; } virtual Vehicle *copy() const { return new Car; } virtual ~Car() {} private: string name;};class Bike : public Vehicle { public: Bike() { name = "bike"; } virtual string getName() { return name; } virtual Vehicle *copy() const { return new Bike; } virtual ~Bike() {} private: string name;};class VehicleSurrogate { public: VehicleSurrogate() : p(0) {} VehicleSurrogate(const Vehicle &v) : p(v.copy()) {} VehicleSurrogate(const VehicleSurrogate &vs) : p(vs.p ? vs.p->copy() : 0) {} VehicleSurrogate &operator=(const VehicleSurrogate &vs) { if (this != &vs) //在删除p之前一定得记得判断vs和this是不是同一个,否则会有问题 { delete p; p = vs.p->copy(); } return *this; } string getName() { return p->getName(); } ~VehicleSurrogate() { delete p; } private: Vehicle *p;};void right_code(vector<VehicleSurrogate> &parkinglot) { Car c; parkinglot.push_back(c);}int main() { vector<VehicleSurrogate> parkinglot; right_code(parkinglot); cout << parkinglot[0].getName() << endl; return 0;}
car
所以,这种代理对象的模式实际上就是对对象的封装,同时处理他们对他们进行资源管理,从而防止指针对象指向未被析构的对象地址,已经内存泄露的问题。
示例来自
1 0
- [c++]代理对象模式
- [c++]代理对象模式
- 【C++】Chapter4:代理模式
- Objective C--代理模式
- Objective C 代理模式
- Objective-c 代理模式
- Object-C代理模式
- <设计模式可复用面向对象软件的基础> [2.7]、代理模式(C#)
- Proxy 代理对象结构模式
- javascript 对象结构 代理模式
- 控制对象访问--代理模式
- 代理模式(Proxy)-对象结构型模式
- 面向对象设计模式---之代理模式
- 面向对象设计模式---之代理模式
- 代理模式-对象结构型模式
- Proxy 代理模式 对象结构型模式
- 代理模式 -- 通过代理对象间接访问对象
- 设计模式之代理模式(C++)
- Flip Game II
- Andoid 总线设计 (一)
- 学习笔记——自定义EditText右侧带图片控件
- 异步线程处理bitmap
- java的设计模式——装饰模式
- [c++]代理对象模式
- iOS之Reachability和AFN判断网络连接状态
- Velocity源码分析以及Spring集成
- CSS——块&行标签
- 利用handler的延时实现app载入时loading
- ubantu 15.10 chrome 开启全局代理设置
- poj1318
- 我的VIM 配置 part2
- 全国计算机二级C 错题/知识点整理