7 Proxy

Davids 2025-08-10 05:16:12
Categories: > > Tags:

代理模式:

有些对象由于一些原因(创建开销大、需要控制安全操作、需要进程外的访问),直接访问会给访问者造成麻烦。
为其他对象提供一种代理,以控制对这个对象的访问(隔离、权限控制)。(如 UE 的渲染 proxy )
设计非常自由,主要就是通过中间层,达到访问隔离的效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Abstract {
virtual void DoSomething();
Abstract ~Abstract(){}
};

class AbstractProxy {
Abstract* m_Abstract;
virtual void RequestDoSomething(){
// if (!access)
// {
// return;
// }
...
m_Abstract->DoSomething();
}
Abstract ~Abstract(){}
};

class User {
private:
AbstractProxy* m_AbstractProxy;
public:
void Do(){
m_AbstractProxy->RequestDoSomething();
}
};