zoukankan      html  css  js  c++  java
  • 猫、老鼠、主人联动问题

    程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒

    要求: 

    1.要有联动性,老鼠和主人的行为是被动的。

    2.考虑可扩展性,猫的叫声可能引起其他联动效应。

     
    整个思路是采用订阅者设计模式(观察者模式),具体怎么叫就随大家想法。我采用Windows的应用程序讲解
     
    下面是发布者抽象模块
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace InterviewDemo
    {
        public delegate void testHandler();
        public abstract class Pubish
        {
            public event testHandler handler;
            public void Activtion()
            {
                if (handler != null)
                {
                    this.handler();
                }
            }
        }
    }
    
    下面是订阅者抽象模块
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace InterviewDemo
    {
        public abstract class Subscription
        {
            public Subscription(Pubish _pubish)
            {
                _pubish.handler += new testHandler(_pubish_handler);
            }
    
            public virtual void _pubish_handler() { }
        }
    }
    

      

      

    猫模块

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InterviewDemo
    {
    
        public class Cat : Pubish
        {
            public string Name { get; private set; }
    
            public Cat(string Name)
            {
                this.Name = Name;
            }
    
            public void Shouted()
            {
                MessageBox.Show(Name + "叫了一声!");
                this.Activtion();
            }
        }
    }
    

    老鼠模块

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InterviewDemo
    {
        public class Mouse : Subscription
        {
            public string Name { get; private set; }
            public Mouse(string Name, Pubish p)
                : base(p)
            {
                this.Name = Name;
            }
            public override void _pubish_handler()
            {
                MessageBox.Show(Name + "听到叫声");
            }
    
        }
    }
    

    主人模块

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InterviewDemo
    {
    
        public class Master : Subscription
        {
    
            public string Name { get; private set; }
    
            public Master(string Name, Pubish p)
                : base(p)
            {
                this.Name = Name;
            }
            public override void _pubish_handler()
            {
                MessageBox.Show(Name + "听到叫声!");
            }
    
        }
    }
    

    程序实现的按钮事件

            private void button1_Click(object sender, EventArgs e)
            {
                Cat cat = new Cat("猫");
                Mouse mouse = new Mouse("老鼠", cat);
                Master master = new Master("主人", cat);
                cat.Shouted();
            }
     
    考这个的目的是在看大家对订阅者模式的熟知度,前面的发布者、订阅者都采用抽象,方便以后的扩展,
    熟悉此模式后可在很多场合应用。

     

  • 相关阅读:
    Linxu下段错误(segmentation fault)的调试
    n900破解无线路由密钥(wep)
    windows 7 下安装运行VC6
    【转载】W32Dasm反汇编工具使用详解
    用PreCode Snippet代码高亮
    转——别告诉我能懂PPT
    程序员必须掌握的基本正则表达式
    简单之美—软件开发实践者的思考—故事场景1
    xml解析
    Android Frameworktouch事件派发过程深入理解
  • 原文地址:https://www.cnblogs.com/jinacookies/p/1999338.html
Copyright © 2011-2022 走看看