[写在看《大话设计模式》时的遐想]狗猫鼠联动行为的问题(一)

原本关系:


狗猫鼠:

CatA看见狗会跑,CatB看见狗会对叫

MouseA看见猫会跑,MouseB看见猫会对叫


实现如下:


 狗叫了,名叫CatA的猫跑了,名叫CatB的猫叫了(意思就是对叫,表示不怕狗,也就是不跑)
 猫叫了,名叫MouseA的老鼠跑了,名叫MouseB的老鼠叫了(意思就是对叫,表示不怕狗,也就是不跑)


输出显示:

事件发生---->狗DogA叫了

名叫CatA的猫跑了它看见了DogA

名叫CatB的猫叫了它看见了DogA
名叫MouseA的老鼠跑了它看见了CatB

名叫MouseB的老鼠叫了它看见了CatB

事件发生---->猫CatA叫了
名叫MouseA的老鼠跑了它看见了CatA

名叫MouseB的老鼠叫了它看见了CatA

 

定义5个类

Aniaml

Dog: Aniaml

Cat : Aniaml

Mouse : Aniaml

AnimalEvent

public class Animal { public Animal() { } public Animal(string name) { _name = name; } private string _name; protected string Name { get { return _name; } set { _name = value; } } public virtual void Run(){} public virtual void Shout(){} public virtual void Run(AnimalEvent a){} public virtual void Shout(AnimalEvent a){} }

 

public class Dog:Animal { public Dog() : base() { } public Dog(string name) : base(name) { } public delegate void DogShoutEvent(AnimalEvent a); public event DogShoutEvent ShoutBox; public override void Shout() { Console.WriteLine("Dog:" + Name.ToString() + "叫了,汪~~~"); if (ShoutBox != null) { AnimalEvent a = new AnimalEvent(); a.Name = Name; ShoutBox(a); } } //public override void Shout(AnimalEvent a) //{ // Console.WriteLine("Dog:" + Name.ToString() + "叫了,汪~~~"); // if (ShoutBox != null) // { // AnimalEvent a = new AnimalEvent(); // ShoutBox(a); // } //} public delegate void DogRunEvent(); public event DogRunEvent RunBox; public override void Run() { Console.WriteLine("Dog:" + Name.ToString() + "逃跑了,汪555~~~"); if (RunBox != null) { RunBox(); } } //扩展狗遇到了其他动物,会跑 //public override void Run(AnimalEvent a) //{ // Console.WriteLine("Dog:" + Name.ToString() + "逃跑了,汪555~~~,我遇见"+a.Name+"了"); // if (RunBox != null) // { RunBox(); } //} }

public class Cat:Animal { public Cat() : base() { } public Cat(string name) : base(name) { } public delegate void CatShoutEvent(AnimalEvent a); public event CatShoutEvent ShoutBox; public override void Shout() { Console.WriteLine("Cat:" + Name + " 叫,喵~~~"); if (ShoutBox != null) { AnimalEvent aa = new AnimalEvent(); aa.Name = Name; ShoutBox(aa); } } public override void Shout(AnimalEvent a) { Console.WriteLine("Cat:" + Name + " 叫,喵~~~,我遇到" + a.Name + "了"); if (ShoutBox != null) { AnimalEvent aa=new AnimalEvent (); aa.Name=Name; ShoutBox(aa); } } public override void Run() { Console.WriteLine("Cat:" + Name + "逃跑了,喵555~~~"); } public override void Run(AnimalEvent a) { Console.WriteLine("Cat:" + Name + "逃跑了,喵555~~~,我遇到" + a.Name + "了"); } }

public class Mouse:Animal { public Mouse() : base() { } public Mouse(string name) : base(name) { } public override void Shout(AnimalEvent a) { Console.WriteLine("Mouse:" + Name + "叫了,吱~~~,我遇到" + a.Name + "了"); } public override void Run(AnimalEvent a) { Console.WriteLine("Mouse:" + Name + "逃跑了,吱555~~~,我遇到" + a.Name + "了"); } public override void Shout() { Console.WriteLine("Mouse:" + Name + "叫了,吱~~~"); } public override void Run() { Console.WriteLine("Mouse:" + Name + "逃跑了,吱555~~~"); } }

public class AnimalEvent { public AnimalEvent() { } private string _name; public string Name { get { return _name; } set { _name = value; } } }

控制台代码:

Dog dogA = new Dog("DogA"); Cat CatA = new Cat("CatA"); Cat CatB = new Cat("CatB"); Mouse MouseA = new Mouse("MouseA"); Mouse MouseB = new Mouse("MouseB"); CatA.ShoutBox += new Cat.CatShoutEvent(MouseA.Run); CatA.ShoutBox += new Cat.CatShoutEvent(MouseB.Shout); CatB.ShoutBox += new Cat.CatShoutEvent(MouseA.Run); CatB.ShoutBox += new Cat.CatShoutEvent(MouseB.Shout); dogA.ShoutBox += new Dog.DogShoutEvent(CatA.Run); dogA.ShoutBox += new Dog.DogShoutEvent(CatB.Shout); Console.Write("事件发生--->"); dogA.Shout(); Console.Write("事件发生--->"); CatA.Shout(); Console.ReadLine();

当前功能可以实现,但是,

现在是狗,猫,鼠
如果有10条狗,Dog1到10
6条猫,Cat1到6
8只老鼠,Mouse1到8
如果出现行为的特殊化 ,也就是说,Cat1看到Dog1、2、6、8跑,其他都是叫;Mouse1看到Cat3、4、5跑,其他叫,
或者
行为出现数量化 ,也就是说CAT1看到DOG1、3、5会跑,看到DOG2会跳,等其他的行为
那这个事件添加的方式感觉相当不太好,等待以后重构


你可能感兴趣的:(设计模式,String,null,Class,扩展)