代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问。
1、远程代理:为一个对象在不同的地址空间提供局部代表,这样可以隐藏一个对象存在于不同地址空间的事实。
代码
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Proxy
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 SchoolGirl xuejiao = new SchoolGirl();
12 xuejiao.Name = "李雪娇";
13 Proxy daili = new Proxy(xuejiao);
14 daili.GiveDolls();
15 daili.GiveFlowers();
16 Console.ReadLine();
17 }
18 }
19 public interface GiveGift
20 {
21 void GiveDolls();
22 void GiveFlowers();
23 }
24 public class SchoolGirl
25 {
26 private string name;
27
28 public string Name
29 {
30 get { return name; }
31 set { name = value; }
32 }
33
34 }
35 public class Persuit:GiveGift
36 {
37 SchoolGirl mm;
38 public Persuit(SchoolGirl mm)
39 {
40 this.mm = mm;
41 }
42 public void GiveDolls()
43 {
44 Console.WriteLine(mm.Name+"送你洋娃娃");
45 }
46 public void GiveFlowers()
47 {
48 Console.WriteLine(mm.Name+"送你花");
49 }
50 }
51 public class Proxy : GiveGift
52 {
53 Persuit gg;
54 public Proxy(SchoolGirl mm)
55 {
56 gg = new Persuit(mm); //通过代理实例化追求者
57 }
58 public void GiveDolls()
59 {
60 gg.GiveDolls();
61 }
62 public void GiveFlowers()
63 {
64 gg.GiveFlowers();
65 }
66 }
67 }
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace Proxy
6 {
7 class Program
8 {
9 static void Main(string[] args)
10 {
11 SchoolGirl xuejiao = new SchoolGirl();
12 xuejiao.Name = "李雪娇";
13 Proxy daili = new Proxy(xuejiao);
14 daili.GiveDolls();
15 daili.GiveFlowers();
16 Console.ReadLine();
17 }
18 }
19 public interface GiveGift
20 {
21 void GiveDolls();
22 void GiveFlowers();
23 }
24 public class SchoolGirl
25 {
26 private string name;
27
28 public string Name
29 {
30 get { return name; }
31 set { name = value; }
32 }
33
34 }
35 public class Persuit:GiveGift
36 {
37 SchoolGirl mm;
38 public Persuit(SchoolGirl mm)
39 {
40 this.mm = mm;
41 }
42 public void GiveDolls()
43 {
44 Console.WriteLine(mm.Name+"送你洋娃娃");
45 }
46 public void GiveFlowers()
47 {
48 Console.WriteLine(mm.Name+"送你花");
49 }
50 }
51 public class Proxy : GiveGift
52 {
53 Persuit gg;
54 public Proxy(SchoolGirl mm)
55 {
56 gg = new Persuit(mm); //通过代理实例化追求者
57 }
58 public void GiveDolls()
59 {
60 gg.GiveDolls();
61 }
62 public void GiveFlowers()
63 {
64 gg.GiveFlowers();
65 }
66 }
67 }