"Proxy"" 顾名思义 "代理"的意思,应属于结构型模式
Proxy模式在现实当中的场景
例如:有一个应届毕业生到深圳找工作,到深圳后没房子住,于是打算去找房子,但是深圳这么大,不可能一个人把整个深圳的房子去找一遍.于是他就跑到了附近的租售公司打听下附近有没有房子租. 这里所说的租售公司相当于 "代理" ,也就是中间层.
场景演示代码如下:
using System;
abstract class Person //人 抽象角色
{
abstract public void Findhouse();//找房子住
}
![](/Images/OutliningIndicators/None.gif)
class FreshPerson : Person //应届毕业生
{
override public void Findhouse()//找房子住
{
Console.WriteLine("应届毕业生找房子住");
}
}
![](/Images/OutliningIndicators/None.gif)
class Proxy : Person //租房中介机构(代理角色)
{
FreshPerson freshperson; //应届毕业生
![](/Images/OutliningIndicators/InBlock.gif)
override public void Findhouse()//找房子
{
if (freshperson == null) //如果为空
freshperson = new FreshPerson();
![](/Images/OutliningIndicators/InBlock.gif)
preFind();//找房子前
freshperson.Findhouse();//帮助应届毕业生找房子
postFind();//找房子后
}
![](/Images/OutliningIndicators/InBlock.gif)
public void preFind()
{
Console.WriteLine("找房子前,我在玩电脑");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void postFind()
{
Console.WriteLine("帮助应届毕业生找房子后,我就去睡觉");
}
}
![](/Images/OutliningIndicators/None.gif)
public class Client
{
public static void Main(string[] args)
{
Proxy p = new Proxy(); //(代理角色)中介机构
//Person p = new Proxy();
p.Findhouse();//帮助应届毕业生找房子
}
}
以上演示了简单的Proxy模式在现实当中的例子.为什么要用到Proxy模式?
一:应届毕业生不可能把整个深圳的房子找遍,这个时候就会碰到困难.
二:为了解决困难 现实当中的Proxy模式油然而升,也就出现了中介机构,也就是程序上的"代理".
三:在我们开发程序当中,有时碰到直接调用对象的方法会不太好,所以很多人想到了用间接的写个类再去访问对象的方法.
这种间接的方式就是Proxy.
Proxy模式在现实当中的场景
例如:有一个应届毕业生到深圳找工作,到深圳后没房子住,于是打算去找房子,但是深圳这么大,不可能一个人把整个深圳的房子去找一遍.于是他就跑到了附近的租售公司打听下附近有没有房子租. 这里所说的租售公司相当于 "代理" ,也就是中间层.
场景演示代码如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
以上演示了简单的Proxy模式在现实当中的例子.为什么要用到Proxy模式?
一:应届毕业生不可能把整个深圳的房子找遍,这个时候就会碰到困难.
二:为了解决困难 现实当中的Proxy模式油然而升,也就出现了中介机构,也就是程序上的"代理".
三:在我们开发程序当中,有时碰到直接调用对象的方法会不太好,所以很多人想到了用间接的写个类再去访问对象的方法.
这种间接的方式就是Proxy.