设计模式之观察者模式——猫抓老鼠1
作者:互联网
设计模式之观察者模式——猫抓老鼠:
问题描述:
通过猫抓老鼠的例子进行了解观察者模式,首先的初始代码为(有三个类):
using System;
/// <summary>
/// 观察者设计模式——猫抓老鼠
/// </summary>
namespace Unity_Observe_mode
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("加菲", "黄色");
Mouse mouse1 = new Mouse("米奇", "白色");
Mouse mouse2 = new Mouse("米糊", "灰色");
cat.CatComing(mouse1,mouse2);//猫的状态发生改变
Console.ReadKey();
}
}
}
猫类:
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 观察者设计模式——猫抓老鼠
/// </summary>
namespace Unity_Observe_mode
{
/// <summary>
/// 猫类
/// </summary>
class Cat
{
private string name;
private string color;
public Cat(string name,string color)
{
this.name = name;
this.color = color;
}
/// <summary>
/// 猫的状态发生改变(被观察者状态发生改变)
/// </summary>
public void CatComing(Mouse mouse1,Mouse mouse2)
{
Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵!!");
mouse1.RunAway();
mouse2.RunAway();
}
}
}
老鼠类:
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 观察者设计模式——猫抓老鼠
/// </summary>
namespace Unity_Observe_mode
{
/// <summary>
/// 老鼠类-观察者类
/// </summary>
class Mouse
{
private string name;
private string color;
public Mouse(string name,string color)
{
this.name = name;
this.color = color;
}
public void RunAway()
{
Console.WriteLine(color + "的老鼠" + name + "说:老猫来了,赶紧跑! ");
}
}
}
分析:
首先,我们编写了三个类,其中猫为被观察者,而老鼠为观察者,当猫状态发生改变时,老鼠也相应的发生改变,如上代码便做到了这一点。
但是这种方案存在一个缺点:
如下代码:
public void CatComing(Mouse mouse1,Mouse mouse2,Mouse mouse3)
{
Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵!!");
mouse1.RunAway();
mouse2.RunAway();
mouse3.RunAway();
}
Mouse mouse1 = new Mouse("米奇", "白色");
Mouse mouse2 = new Mouse("米糊", "灰色");
Mouse mouse3 = new Mouse("米啊", "红色色");
cat.CatComing(mouse1,mouse2,mouse3);//猫的状态发生改变
当新的观察者加入时,需要在猫类的方法中进行更改,这样会使代码越来越繁琐,而且我们希望写好一个猫类后尽量少改动其代码(对比初始代码)
通过委托进行优化:
我们在猫类中定义一个委托,然后将老鼠的方法加入委托中:
using System;
/// <summary>
/// 观察者设计模式——猫抓老鼠
/// </summary>
namespace Unity_Observe_mode
{
class Program
{
static void Main(string[] args)
{
Cat cat = new Cat("加菲", "黄色");
Mouse mouse1 = new Mouse("米奇", "白色");
cat.catCome += mouse1.RunAway;
Mouse mouse2 = new Mouse("米糊", "灰色");
cat.catCome += mouse2.RunAway;
Mouse mouse3 = new Mouse("米啊", "红色色");
cat.catCome += mouse3.RunAway;
//cat.CatComing(mouse1,mouse2,mouse3);//猫的状态发生改变,在cat中调用了观察者的方法,当观察者发生改变时,需要同时修改被观察者的代码,代码耦合性高了,不好!!
cat.CatComing();
Console.ReadKey();
}
}
}
猫类:
using System;
using System.Collections.Generic;
using System.Text;
/// <summary>
/// 观察者设计模式——猫抓老鼠
/// </summary>
namespace Unity_Observe_mode
{
/// <summary>
/// 猫类
/// </summary>
class Cat
{
private string name;
private string color;
public Cat(string name,string color)
{
this.name = name;
this.color = color;
}
/// <summary>
/// 猫的状态发生改变(被观察者状态发生改变)
/// </summary>
public void CatComing()
{
Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵!!");
if(catCome!=null)
catCome();
}
public Action catCome;//定义一个委托
//public void CatComing(Mouse mouse1,Mouse mouse2,Mouse mouse3)
//{
// Console.WriteLine(color + "的猫" + name + "过来了,喵喵喵!!");
// mouse1.RunAway();
// mouse2.RunAway();
// mouse3.RunAway();
//}
}
}
通过这样的方法降低了代码的耦合性。最后运行效果:
观察者模式—猫抓老鼠1,后面猫抓老鼠2将继续进行优化!
标签:老鼠,name,color,观察者,mouse2,设计模式,Mouse,string 来源: https://blog.csdn.net/Leo__ch/article/details/120524873