using System;
using System. Collections. Generic;
using System. Linq;
using System. Text;
using System. Threading. Tasks;
namespace _012_观察者设计模式_猫捉老鼠 {
class Program {
static void Main ( string [ ] args) {
Cat cat = new Cat ( "加菲猫" , "黄色" ) ;
Mouse mouse1 = new Mouse ( "米奇" , "黑色" , cat) ;
Mouse mouse4 = new Mouse ( "水" , "红色" , cat) ;
cat. CatComing ( ) ;
Console. ReadKey ( ) ;
}
}
class Mouse
{
private string name;
private string color;
public Mouse ( string name, string color, Cat cat)
{
this . name = name;
this . color = color;
cat. CatComeEvent + = RunAway;
}
public void RunAway ( )
{
Console. WriteLine ( color + "的老鼠" + name + "说: 老猫来, 赶紧跑, 我跑, 我使劲跑,我加速使劲跑 ..." ) ;
}
}
class Cat
{
private string name;
private string color;
public Cat ( string name, string color)
{
this . name = name;
this . color = color;
}
public void CatComing ( )
{
Console. WriteLine ( color + "的猫" + name + "过来了,喵喵喵 ..." ) ;
if ( CatComeEvent != null )
CatComeEvent ( ) ;
}
public event Action CatComeEvent;
}
}