观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听一个主题对象,当主题对象发生变换时,会通知所有观察者,使他们能够自动更新自己。
观察者模式中委托事件的应用,极大的弥补了观察者模式的不足。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication22
{
class cat
{
public string name;
public string color;
public cat(string name, string color)
{
this.name = name;
this.color = color;
}
public void Comeing()
{
Console.WriteLine(“一只”+color+“的”+name+“过来了”);
cat1();
}
public Action cat1;
}
class Mouth
{
public string name;
public string color;
public Mouth(string name, string color)
{
this.name = name;
this.color = color;
}
public void Runing()
{
Console.WriteLine(“一只” + color + “的” + name + “说:猫来了,快跑”);
}
}
class Program
{
static void Main(string[] args)
{
cat c = new cat("猫A", "白色");
Mouth m1 = new Mouth("老鼠A","黑色");
c.cat1 += m1.Runing;
Mouth m2 = new Mouth("老鼠B","灰色");
c.cat1 += m2.Runing;
c.Comeing();
Console.ReadKey();
}
}
}
设计模式之观察者模式类和接口实现
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication40
{
//提供保存观察者的容器//定义了一个被观察者
public class See
{
//提供保存观察者的容器
private List li=new List();
private string name;
private string color;
public See(string name, string color)
{
this.name = name;
this.color = color;
}
//提供增加观察者的方法
public void AddSeeman(IObserver ob)
{
li.Add(ob);
}
//提供删除观察者的方法
public void RemoveSeeman(IObserver ob)
{
li.Remove(ob);
}
//对观察者进行通知
public void Update()
{
Console.WriteLine(“老猫出动”);
foreach (IObserver ob in li)
{
if (ob != null)
{
ob.ReceiveAndPint(this);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication40
{
public interface IObserver
{
void ReceiveAndPint(See be);
}
class BeSee:IObserver
{
public string name;
public BeSee(string name)
{
this.name = name;
}
public void ReceiveAndPint(See be)
{
Console.WriteLine(name+"接收到了信息,做出了相应的反应");
}
}
}
using System;
using System.Collections.Generic;//泛型命名空间的集合和容器 system.collection.generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication40
{
class Program
{
static void Main(string[] args)
{
See se = new See(“Tome”, “黑色”);
se.AddSeeman(new BeSee(“舒克”));
se.AddSeeman(new BeSee(“贝塔”));
se.Update();
Console.ReadKey();
}
}
}
using UnityEngine;
using System.Collections;
using System;
public enum DepartmentType
{
Purchasing,
FrontDesk,
Finance
}
//被观察对象
public class XingZ
{
public delegate void DoSome(DepartmentType department, string text);
private event DoSome Send;
public void AddListen(DoSome fun)
{
Send += fun;
}
public void RemoveListen(DoSome fun)
{
Send -= fun;
}
public void TongZhi(DepartmentType department, string text)
{
if (Send != null)
{
Send(department, text);
}
}
}
public interface Observer
{
void Receive(DepartmentType department,string text);
}
//观察者对象
public class CaiGou : Observer
{
public void Receive(DepartmentType department, string text)
{
if (department == DepartmentType.Purchasing)
{
Debug.Log(“这里是采购部,我等会就去买” + text);
}
}
}
//财务
public class Finance : Observed
{
public void Receive(DepartmentType department, string text)
{
if (department == DepartmentType.Finance)
{
Debug.Log(“这里是财务,我等会就批复” + text);
}
}
}
//前台
public class FrontDesk : Observed
{
public void Receive(DepartmentType department, string text)
{
if (department == DepartmentType.FrontDesk)
{
Debug.Log(“这里是前台,我等会就去接待” + text);
}
}
}
public class GG : MonoBehaviour
{
// Use this for initialization
void Start ()
{
XingZ xingz = new XingZ();
CaiGou caiGou = new CaiGou();
Finance finance = new Finance();
FrontDesk frontDesk = new FrontDesk();
xingz.AddListen(caiGou.Receive);
xingz.AddListen(finance.Receive);
xingz.AddListen(frontDesk.Receive);
xingz.TongZhi(DepartmentType.Purchasing,"电脑,配置需求:CPU I7,显卡GTX970,内存8G,主板:技嘉");
xingz.TongZhi(DepartmentType.FrontDesk, "李总,下午安排会议");
//xingz.TongZhi(DepartmentType.Finance, "采购部门电脑购买的款");
}
// Update is called once per frame
void Update ()
{
}
}