观察者模式 代码

ContractedBlock.gifExpandedBlockStart.gifCode
 1None.gif
 2None.gif 1 using System;
 3None.gif 2 using System.Collections.Generic;
 4None.gif 3 using System.Text;
 5None.gif 4 
 6None.gif 5 namespace ClassLibDelegate
 7ExpandedBlockStart.gifContractedBlock.gif 6 dot.gif{
 8InBlock.gif 7    public delegate void SubEventHandler();
 9InBlock.gif 8 
10InBlock.gif 9     public abstract class Subject
11ExpandedSubBlockStart.gifContractedSubBlock.gif10     dot.gif{
12InBlock.gif11         public event SubEventHandler SubEvent;
13InBlock.gif12 
14InBlock.gif13         protected void FireAway()
15ExpandedSubBlockStart.gifContractedSubBlock.gif14         dot.gif{
16InBlock.gif15             if (SubEvent != null)
17ExpandedSubBlockStart.gifContractedSubBlock.gif16             dot.gif{
18InBlock.gif17                 this.SubEvent();
19ExpandedSubBlockEnd.gif18             }

20ExpandedSubBlockEnd.gif19         }

21ExpandedSubBlockEnd.gif20     }

22InBlock.gif21 
23InBlock.gif22     public class Cat : Subject
24ExpandedSubBlockStart.gifContractedSubBlock.gif23     dot.gif{
25InBlock.gif24         public void Cry()
26ExpandedSubBlockStart.gifContractedSubBlock.gif25         dot.gif{
27InBlock.gif26             this.FireAway();
28ExpandedSubBlockEnd.gif27         }

29ExpandedSubBlockEnd.gif28     }

30InBlock.gif29 
31InBlock.gif30     public abstract class Observer
32ExpandedSubBlockStart.gifContractedSubBlock.gif31     dot.gif{
33InBlock.gif32         public Observer(Subject sub)
34ExpandedSubBlockStart.gifContractedSubBlock.gif33         dot.gif{
35InBlock.gif34             sub.SubEvent += new SubEventHandler(sub_SubEvent);
36ExpandedSubBlockEnd.gif35         }

37InBlock.gif36 
38InBlock.gif37         public abstract void sub_SubEvent();
39InBlock.gif38        
40ExpandedSubBlockEnd.gif39     }

41InBlock.gif40 
42InBlock.gif41     public class Mouse : Observer
43ExpandedSubBlockStart.gifContractedSubBlock.gif42     dot.gif{
44InBlock.gif43         private string name;
45InBlock.gif44         public Mouse(string name, Subject sub)
46InBlock.gif45             : base(sub)
47ExpandedSubBlockStart.gifContractedSubBlock.gif46         dot.gif{
48InBlock.gif47             this.name = name;
49ExpandedSubBlockEnd.gif48         }

50InBlock.gif49 
51InBlock.gif50         public override void sub_SubEvent()
52ExpandedSubBlockStart.gifContractedSubBlock.gif51         dot.gif{
53InBlock.gif52             
54ExpandedSubBlockEnd.gif53         }

55ExpandedSubBlockEnd.gif54     }

56InBlock.gif55 
57InBlock.gif56     public class Master : Observer
58ExpandedSubBlockStart.gifContractedSubBlock.gif57     dot.gif{
59InBlock.gif58         public Master(Subject sub)
60InBlock.gif59             : base(sub)
61ExpandedSubBlockStart.gifContractedSubBlock.gif60         dot.gif{
62InBlock.gif61             
63ExpandedSubBlockEnd.gif62         }

64InBlock.gif63 
65InBlock.gif64         public override void sub_SubEvent()
66ExpandedSubBlockStart.gifContractedSubBlock.gif65         dot.gif{
67InBlock.gif66             
68ExpandedSubBlockEnd.gif67         }

69ExpandedSubBlockEnd.gif68     }

70ExpandedBlockEnd.gif69 }

71None.gif70 
72None.gif
73None.gif
74None.gif
75None.gif
76None.gif
77None.gif

转载于:https://www.cnblogs.com/March/archive/2008/08/30/1279956.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值