猫叫了,所有老鼠开始逃跑---经典面试题

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1ExpandedBlockStart.gifContractedBlock.gif/**//*
 2 * 题目:
 3 * 猫叫了,所有老鼠开始逃跑,主人被惊醒,请用OO的思想描绘此过程
 4 * 1,老鼠跟主人是被动的
 5 * 2,要考虑联动性与扩展性
 6 */

 7using System;
 8using System.Collections.Generic;
 9using System.Text;
10
11ExpandedBlockStart.gifContractedBlock.gifnamespace CatCry {
12
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*委托,当猫叫时将信息传给所有观察者*/
14    public delegate void CatCryEventHandler(object sender, EventArgs e);
15
16ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*被观察对象*/
17ExpandedSubBlockStart.gifContractedSubBlock.gif    abstract class Subject {
18        public event CatCryEventHandler CatCryEvent;//猫叫时的事件
19ExpandedSubBlockStart.gifContractedSubBlock.gif        public void Cry(EventArgs e) {
20            this.CatCryEvent(this, e);
21        }

22ExpandedSubBlockStart.gifContractedSubBlock.gif        public void Cry() {
23            //不接收猫叫时发出的信息
24            this.CatCryEvent(thisnew EventArgs());
25        }

26    }

27
28ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*观察者*/
29ExpandedSubBlockStart.gifContractedSubBlock.gif    abstract class Observer {
30ExpandedSubBlockStart.gifContractedSubBlock.gif        public Observer() { }
31ExpandedSubBlockStart.gifContractedSubBlock.gif        public Observer(Subject subject) {
32            subject.CatCryEvent += new CatCryEventHandler(this.Notified);
33        }

34        //当猫叫时做出的反应(当收到通知时)
35        abstract protected void Notified(object sender, EventArgs e);
36    }

37
38ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*定义猫作为被观察对象*/
39ExpandedSubBlockStart.gifContractedSubBlock.gif    class Cat : Subject {
40ExpandedSubBlockStart.gifContractedSubBlock.gif        public Cat() { Console.WriteLine("猫叫了"); }
41    }

42
43ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*定义老鼠作为观察者*/
44ExpandedSubBlockStart.gifContractedSubBlock.gif    class Mouse : Observer {
45        string mouseName;
46ExpandedSubBlockStart.gifContractedSubBlock.gif        public Mouse() { }
47        public Mouse(string mouseName, Subject subject)
48ExpandedSubBlockStart.gifContractedSubBlock.gif            : base(subject) {
49            this.mouseName = mouseName;
50        }

51ExpandedSubBlockStart.gifContractedSubBlock.gif        override protected void Notified(object sender, EventArgs e) {
52            Console.WriteLine(this.mouseName + "开始逃跑");
53        }

54    }

55
56ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//*定义主人作为观察者*/
57ExpandedSubBlockStart.gifContractedSubBlock.gif    class Marster : Observer {
58ExpandedSubBlockStart.gifContractedSubBlock.gif        public Marster() { }
59ExpandedSubBlockStart.gifContractedSubBlock.gif        public Marster(Subject subject) : base(subject) { }
60ExpandedSubBlockStart.gifContractedSubBlock.gif        override protected void Notified(object sender, EventArgs e) {
61            Console.WriteLine("主人被惊醒了");
62        }

63    }

64
65ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
66    /// 功能:用观察者模式实现
67    /// 作者:allnen
68    /// 时间:2008-6-5
69    /// </summary>

70ExpandedSubBlockStart.gifContractedSubBlock.gif    class Program {
71ExpandedSubBlockStart.gifContractedSubBlock.gif        static void Main(string[] args) {
72            Cat cat = new Cat();
73            Mouse m1 = new Mouse("老鼠1", cat);
74            Mouse m2 = new Mouse("老鼠2", cat);
75
76            Marster marster = new Marster(cat);
77            cat.Cry();//猫叫了
78
79        }

80    }

81}

82

转载于:https://www.cnblogs.com/seerlin/archive/2008/09/06/1285812.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值