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();

        }

    }

相关文章推荐

12 Interesting C Interview Questions and Answers

In this article, we will discuss some interesting problems on C language that can help students to b...

C++ outline and interview questions (1): Variable

C++ outline and interview questions (1): Variable

C++ Interview Questions and Answers - Part 01

0001问、头文件中的“#ifndef/#define/#endif”干什么用? 0001答:防止头文件被重复引用。...

Core Java Interview Questions and Answers

Whether you are a fresher or highly experienced professional, core java plays a vital role in any ...

C/C++ Programming interview questions and answers

What is encapsulation?? Containing and hiding information about an object, such as internal data ...

10 Hibernate Interview Questions and Answers for Java J2EE Programmers

在Java J2EE方面进行面试时,常被问起的Hibernate面试问题,大多都是针对基于Web的企业级应用开发者的角色的。Hibernate框架在Java界的成功和高度的可接受性使得它成为了Java...

Top 10 Java Serialization Interview Questions and Answers

转载自:原文地址 Top 10 Java Serialization Interview Questions and Answers What is Serializa...

English job interview Questions and Answers

"What are your goals for the future?" or "Where do you see yourself in five years?"   Don't di...

11 Basic Linux Interview Questions and Answers

A new article in this section (Linux Interview) will be posted on every weekend. The initiative take...

Answers to some common problems and questions about Xen

Answers to some common problems and questions about Xen Contents I'd like to contribute to the ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Two Interview questions! about event and Observer mode
举报原因:
原因补充:

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