using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Car cat = new Car();
Mouse mouse1 = new Mouse("mouse1", cat);
Mouse mouse2 = new Mouse("mouse2", cat);
Master master = new Master(cat);
cat.Cry();
Console.ReadLine();
}
}
interface Observer //观察者的响应(针对那一个对象)
{
void Reaponse();
}
interface Subject//针对那些观察者(也就是遇到的对象)
{
void AimAt(Observer obs);
}
class Mouse : Observer
{
private string Name;
public Mouse(string name, Subject sb)
{
this.Name = name;
sb.AimAt(this);
}
public void Reaponse()
{
Console.WriteLine(Name + " attempt to escape!");
}
}
class Master : Observer
{
public Master(Subject sb)
{
sb.AimAt(this);
}
public void Reaponse()
{
Console.WriteLine("Host waken!");
}
}
class Car : Subject//这就是猫的响应和执行的对象
{
ArrayList arrs;
public Car()
{
arrs = new ArrayList();
}
public void AimAt(Observer obs)//它的对象就是叫一声
{
arrs.Add(obs);
}
public void Cry()
{
Console.WriteLine("Cat cryed!");
foreach (Observer lsg in this.arrs)
{
lsg.Reaponse();
}
}
}
}
联系客服