代理模式学习

(2013/10/28)

今天学习了(大话设计模式)代理模式:

主要功能:

1.远程代理,也就是为一个对象在不同的地址空间提供局部代理。这样可以隐藏一个对象存在不同的地址空间的事实【DP】。

如:WebService在。NET中的应用,在引用一个WebService,此时会在项目中生成出一个WebReference的文件夹和一些文件,其实它们就是代理,这就是使用客户端程序调用代理就可以解决远程访问的问题。

2.虚拟代理,就是根据需要创建开销很大的对象。通过它来存放实例化需要很长时间的真实对象【DP】。

如:打开一个很大的HTML网页时,里面可能有很多的文字和图片,但你还是可以很快打开它,此时你所看到的所以文字,但是图片却是一张一张地下载后才能看到。那些未打开的图片框,就是通过虚拟代理来替代了真实的图片,此时代理存储了真实图片的路径和尺寸。

3.安全代理,用来控制真实对象访问时的权限【DP】。

如:一般用于对象应该有不同访问权限的时候。

4.智能指引,是指调用真实对象时,代理处理另外一些事。

如:计算机真实对象的引用次数,这样当该对象没有引用时,可以自动释放它;或当第一次引用一个持久对象时,将它装入内存;或在访问一个实际对象前,检查是否已经锁定它,以确保其他对象不能改变它。它们都是通过代理在访问一个对象时附加一些内务处理。

关系:

真实实体Pursuit 追求者

代理      Proxy代理

都需要继承接口 IGiveGife

然后通过代理,执行真正实体的操作。

代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _7代理模式Proxy
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             SchoolGirl jiaojiao = new SchoolGirl();
13             jiaojiao.Name = "李娇娇";
14 
15             Proxy daili = new Proxy(jiaojiao);
16             daili.GiveChocolate();
17             daili.GiveDolls();
18             daili.GiveFlowers();
19 
20             Console.ReadKey();
21         }
22     }
23 }
IGiveGife.cs

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _7代理模式Proxy
 7 {
 8     class Proxy:IGiveGife
 9     {
10         Pursuit gg;
11         public Proxy(SchoolGirl mm)
12         {
13             gg = new Pursuit(mm);
14         }
15         public void GiveDolls()
16         {
17             gg.GiveDolls();
18         }
19 
20         public void GiveFlowers()
21         {
22             gg.GiveFlowers();
23         }
24 
25         public void GiveChocolate()
26         {
27             gg.GiveChocolate();
28         }
29     }
30 }
Proxy

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _7代理模式Proxy
{
    class Pursuit:IGiveGife
    {
        SchoolGirl mm;
        public Pursuit(SchoolGirl mm)
        {
            this.mm = mm;
        }
        public void GiveDolls()
        {
            Console.WriteLine(mm.Name+"送你洋娃娃");
        }

        public void GiveFlowers()
        {
            Console.WriteLine(mm.Name+"送你鲜花");
        }

        public void GiveChocolate()
        {
            Console.WriteLine(mm.Name+"送你巧克力");
        }
    }
}
Pursuit
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _7代理模式Proxy
 7 {
 8     class SchoolGirl
 9     {
10         private string name;
11         /// <summary>
12         /// 名称
13         /// </summary>
14         public string Name
15         {
16             get { return name; }
17             set { name = value; }
18         }
19     }
20 }
SchoolGirl
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace _7代理模式Proxy
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             SchoolGirl jiaojiao = new SchoolGirl();
13             jiaojiao.Name = "李娇娇";
14 
15             Proxy daili = new Proxy(jiaojiao);
16             daili.GiveChocolate();
17             daili.GiveDolls();
18             daili.GiveFlowers();
19 
20             Console.ReadKey();
21         }
22     }
23 }
Program

 

 

 

 

 

posted @ 2013-10-28 00:57  偏执的放纵  阅读(250)  评论(0编辑  收藏  举报