惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 常大波

{转载} 面试技巧 项目管理知识 程序员九重镜界,很老的今天刚刚翻出来 我来证明越南的ZingChat2是腾讯公司开发的 删除Flash控件的 Flash9e.ocx和FlashUtil9e.exe C#入门代码集 JS脚本判断是否支持Cookie,C#读取设置Cookie SRE(Simple Rule Engine) Document JavaScript工具 - 常大波 - 博客园 SQLite NxBRE 学习笔记1 [转] SQL视图查出SqlServer的数据库字典---适用于 SQL2K 和SQL2005 对于SQL2008不适用 乡音 动态表名的查询SQL Delegate 委托 C# 来到深圳找工作。 获取键盘键值 呼叫中心(CallCenter)的发展 Message Queue(消息队列)介绍与应用---转自CSDN
有关“猫”的设计题目,开阔思维。
常大波 · 2007-03-10 · via 博客园 - 常大波

 程序设计: 猫大叫一声,所有的老鼠都开始逃跑,主人被惊醒。(C#语言)

要求:  1.要有联动性,老鼠和主人的行为是被动的。
               2.考虑可扩展性,猫的叫声可能引起其他联动效应。

参考答案:

 要点:1. 联动效果,运行代码只要执行Cat.Cryed()方法。2. 对老鼠和主人进行抽象
评分标准: <1>.构造出Cat、Mouse、Master三个类,并能使程序运行(2分)
                  <2>从Mouse和Master中提取抽象(5分)
                  <3>联动效应,只要执行Cat.Cryed()就可以使老鼠逃跑,主人惊醒。(3分)

 1 using System;
 2 using System.Collections;
 3    public interface Observer 
 4    
 5        void Response();    //观察者的响应,如是老鼠见到猫的反映 
 6    }
 
 7    public interface Subject 
 8    
 9        void AimAt(Observer obs);  //针对哪些观察者,这里指猫的要扑捉的对象---老鼠 
10    }
 
11    public class Mouse : Observer 
12    
13        private string name; 
14        public Mouse(string name, Subject subj) 
15        {           
16            this.name = name; 
17            subj.AimAt(this); 
18        }
 
19        
20        public void Response() 
21        
22            Console.WriteLine(name +  "  attempt to escape!"); 
23        }
 
24    }
 
25    public class Master : Observer 
26    {   
27        public Master(Subject subj) 
28        {           
29            subj.AimAt(this); 
30        }
 
31        
32        public void Response() 
33        
34            Console.WriteLine("Host waken!"); 
35        }
  
36    }
 
37  
38    public class Cat : Subject 
39    
40        private ArrayList observers; 
41        public Cat() 
42        {   
43            this.observers = new ArrayList(); 
44        }
 
45        public void AimAt(Observer obs) 
46        
47            this.observers.Add(obs); 
48        }
 
49        public void Cry() 
50        
51            Console.WriteLine("Cat cryed!"); 
52            foreach (Observer obs in this.observers) 
53            
54                obs.Response(); 
55            }
 
56        }
 
57    }
 
58    class MainClass 
59    {       
60        static void Main(string[] args) 
61        
62            Cat cat = new Cat(); 
63            Mouse mouse1 = new Mouse("mouse1", cat); 
64            Mouse mouse2 = new Mouse("mouse2", cat); 
65            Master master = new Master(cat); 
66            cat.Cry(); 
67        }
 
68    }
 


//---------------------------------------------------------------------------------------------

设计方法二: 使用event -- delegate设计.. 

 1  using System;  
 2    public delegate void SubEventHandler(); 
 3    public abstract class Subject 
 4    
 5        public event SubEventHandler SubEvent; 
 6        protected void FireAway() 
 7        
 8            if (this.SubEvent != null
 9                this.SubEvent(); 
10        }
   
11    }
 
12    public class Cat : Subject 
13    {  
14        public void Cry() 
15        
16            Console.WriteLine("cat cryed."); 
17            this.FireAway(); 
18        }
 
19    }
 
20    public abstract class Observer 
21    
22        public Observer(Subject sub) 
23        
24            sub.SubEvent += new SubEventHandler(Response); 
25        }
 
26        public abstract void Response();    
27    }
 
28    public class Mouse : Observer 
29    
30        private string name; 
31        public Mouse(string name, Subject sub) : base(sub) 
32        {   
33            this.name = name; 
34        }
 
35        public override void Response() 
36        
37            Console.WriteLine(name + " attempt to escape!"); 
38        }
 
39    }
 
40    public class Master : Observer 
41    
42        public Master(Subject sub) : base(sub){} 
43        public override void Response() 
44        
45            Console.WriteLine("host waken"); 
46        }
 
47    }
 
48    class Class1 
49    
50        static void Main(string[] args) 
51        
52            Cat cat = new Cat(); 
53            Mouse mouse1 = new Mouse("mouse1", cat); 
54            Mouse mouse2 = new Mouse("mouse2", cat); 
55            Master master = new Master(cat); 
56            cat.Cry(); 
57        }
 
58    }

59


-----------------
我个人倾向于 第二中方法。
终于知道怎么发 代码帖子了,以后我会多多努力的。