• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
gooliugle
博客园    首页    新随笔    联系   管理    订阅  订阅
大话设计模式读书笔记5----代理模式(Proxy)

代理模式(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 }


 

posted on 2010-04-24 19:31  gooliugle  阅读(296)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3