设计模式 - 代理模式

代理模式:
为其他对象提供一种代理以控制对这个对象的访问。

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace ConsoleApplication1
  7 {
  8 
  9     interface IActions
 10     {
 11         void GiveFlowers();
 12         void GiveFruits();
 13         void GivePerfume();
 14     }
 15 
 16     class SchoolGirl
 17     {
 18         private string name;
 19 
 20         public SchoolGirl(string name)
 21         {
 22             this.name = name;
 23         }
 24 
 25         public string Name
 26         {
 27             get
 28             {
 29                 return this.name;
 30             }
 31             set
 32             {
 33                 this.name = value;
 34             }
 35         }
 36     }
 37 
 38     class Pursuit : IActions
 39     {
 40         private SchoolGirl girl;
 41         private string name;
 42 
 43         public Pursuit(string name, SchoolGirl girl)
 44         {
 45             this.name = name;
 46             this.girl = girl;
 47         }
 48 
 49         public void GiveFlowers()
 50         {
 51             Console.WriteLine(this.name + " give flowers to " + girl.Name);
 52         }
 53 
 54         public void GiveFruits()
 55         {
 56             Console.WriteLine(this.name + " give fruits to " + girl.Name);
 57         }
 58 
 59         public void GivePerfume()
 60         {
 61             Console.WriteLine(this.name + " give perfume to " + girl.Name);
 62         }
 63     }
 64 
 65     class Proxy : IActions
 66     {
 67         private Pursuit pursuit;
 68 
 69         public Proxy(Pursuit pursuit)
 70         {
 71             this.pursuit = pursuit;
 72         }
 73 
 74         public void GiveFlowers()
 75         {
 76             this.pursuit.GiveFlowers();
 77         }
 78 
 79         public void GiveFruits()
 80         {
 81             this.pursuit.GiveFruits();  
 82         }
 83 
 84         public void GivePerfume()
 85         {
 86             this.pursuit.GivePerfume();
 87         }
 88     }
 89 
 90     class Program
 91     {
 92         static void Main(string[] args)
 93         {
 94             SchoolGirl girl = new SchoolGirl("Lucy");
 95             Pursuit jim = new Pursuit("Jim", girl);
 96 
 97             Proxy proxy = new Proxy(jim);
 98 
 99             proxy.GiveFlowers();
100             proxy.GiveFruits();
101             proxy.GivePerfume();
102 
103             Console.ReadLine();
104         }
105     }
106 }

猜你喜欢

转载自www.cnblogs.com/zzunstu/p/9298503.html