Two Interview questions! about event and Observer mode

原创 2006年05月29日 18:22:00

The two solution of interview question.

1. use observer modal

 public interface Observer

{

     void Response();    //the observer’s response

 }

 public class Cat : Subject

 {

private ArrayList observers;

        public Cat()

        {  

            this.observers = new ArrayList();

        }

        public void AimAt(Observer obs)

        {

            this.observers.Add(obs);

        }

        public void Cry()

        {

            Console.WriteLine("Cat cryed!");

            foreach (Observer obs in this.observers)

            {

                obs.Response();

            }

        }

    }

    class MainClass

    {      

        static void Main(string[] args)

        {

            Cat cat = new Cat();

            Mouse mouse1 = new Mouse("mouse1", cat);

            Mouse mouse2 = new Mouse("mouse2", cat);

            Master master = new Master(cat);

            cat.Cry();

        }

    }

 

2 使用事件

public delegate void SubEventHandler();

    public abstract class Subject

    {

        public event SubEventHandler SubEvent;

        protected void FireAway()

        {

            if (this.SubEvent != null)

                this.SubEvent();

        }  

    }

    public class Cat : Subject

    { 

        public void Cry()

        {

            Console.WriteLine("cat cryed.");

            this.FireAway();

        }

    }

    public abstract class Observer

    {

        public Observer(Subject sub)

        {

            sub.SubEvent += new SubEventHandler(Response);

        }

        public abstract void Response();   

    }

    public class Mouse : Observer

    {

        private string name;

        public Mouse(string name, Subject sub) : base(sub)

        {  

            this.name = name;

        }

        public override void Response()

        {

            Console.WriteLine(name + " attempt to escape!");

        }

    }

    public class Master : Observer

    {

        public Master(Subject sub) : base(sub){}

        public override void Response()

        {

            Console.WriteLine("host waken");

        }

    }

    class Class1

    {

        static void Main(string[] args)

        {

            Cat cat = new Cat();

            Mouse mouse1 = new Mouse("mouse1", cat);

            Mouse mouse2 = new Mouse("mouse2", cat);

            Master master = new Master(cat);

            cat.Cry();

        }

    }

Core Java Interview Questions and Answers

Whether you are a fresher or highly experienced professional, core java plays a vital role in any ...
  • doctor_who2004
  • doctor_who2004
  • 2014年12月27日 16:30
  • 868

C/C++ Programming interview questions and answers (ZZ)

C/C++ Programming interview questions and answers By Satish Shetty, July 14th, 2004What is encapsula...
  • aclipse
  • aclipse
  • 2008年02月27日 15:06
  • 1105

Python interview questions

15 Essential Python Interview Questions #Python   #Interview     – November 19th 2015   ...
  • screaming
  • screaming
  • 2016年06月01日 10:09
  • 378

Java-Collections Framework学习与总结-PriorityQueue

开发中有时会遇到这样的情况。要求某个调度器去调度一些任务,这些任务放在队列里。任务本身有优先级,调度器要按照优先级去调度队列里的任务,当然也会有新任务不断加入到队列中。         可以类比操作...
  • hsstc
  • hsstc
  • 2015年03月31日 11:49
  • 277

Technical Interview Questions, Answers, and Tips

http://www.technicalinterviewquestions.net/2008/12/questions.html ...
  • bmzydream_007
  • bmzydream_007
  • 2014年02月05日 11:09
  • 755

Coursera Algorithms week1 Interview Questions: Egg drop

Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg break...
  • kikicentaur
  • kikicentaur
  • 2017年07月20日 15:06
  • 102

25道核心JavaScript面试题

原文地址:https://www.toptal.com/javascript/interview-questions在多年的程序设计生涯里,我常常面临着各种新的语言,新的工具。但是语言、工具最终也仅仅...
  • devincob
  • devincob
  • 2017年05月21日 15:16
  • 504

Princeton Algorithms: Part 2 [week 5: Substring Search]

Exercise Question Explanation The correct answer is: 1 2 2 4 5 2 7 5Here is the DFA: 0...
  • jj12345jj198999
  • jj12345jj198999
  • 2014年12月14日 16:13
  • 680

面试时最经常被问到的问题(Frenquently asked interview questions)之Algorithms & Coding篇

 Algorithms & Coding Questions & Answers1、Write a function to print the Fibonacci numbers.-- int fib...
  • mxclxp
  • mxclxp
  • 2004年11月01日 19:19
  • 1693

ocp-380

QUESTION NO: 380 Which two statements about workload capture and replay operations are true? (Choos...
  • xuejiayue1105
  • xuejiayue1105
  • 2015年10月13日 18:05
  • 1150
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Two Interview questions! about event and Observer mode
举报原因:
原因补充:

(最多只允许输入30个字)