观察者模式
2013-12-22 12:58
134 查看
模式名称:观察者模式
生活场景:我们在过马路的时候,总是会去观察红绿灯的状态。是红灯的时候,我们会停下脚步等待,变黄灯的时候我们就准备起步,绿灯我们就过斑马线了。
终极目标:实现自动发送红绿灯状态给每一个斑马线上的人。
1.不假思索的思路:通过类继承的方式来做上面的例子。即:先建立路类;然后派生出高速公路类、市区公路类;然后再高速公路类和市区公路类上分别派生出:小汽车类和公共汽车类。
类结构图:
代码浏览:
namespace Observer
{
/// <summary>
/// 人类
/// </summary>
public class People
{
private string name;
public string Name//字段封装
{
get { return name; }
set { name = value; }
}
public People(string name)
{
this.name = name;
}
/// <summary>
/// 查看红绿灯状态
/// </summary>
public void Watch()
{
Console.WriteLine(name + ":现在红绿灯是神马状态?");
}
}
/// <summary>
/// 红绿灯类
/// </summary>
public class TrafficLight
{
/// <summary>
/// 发送红绿灯状态
/// </summary>
/// <param name="state">红绿灯的状态</param>
public void SendMessage(string state)
{
People people = new People("阿毛");
people.Watch();
Console.WriteLine("红绿灯:"+people.Name + ",我现在是" + state + ",别过斑马线!");
}
}
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
trl.SendMessage("红灯");
Console.ReadKey();
}
}
}
存在问题:给不同的人发送状态,每次都要改变红绿灯类
2.归纳阶段
归纳步骤一
当前目标:实现多人群,且不改变红绿灯类。
思路:把人们的共有特点总结起来,以达到不改变红绿灯类。
类结构图:
代码浏览:
#region 接口声明
/// <summary>
/// 人类显示红绿灯接口
/// </summary>
public interface IPeople
{
void Show(string state);//显示红绿灯状态
}
/// <summary>
/// 红绿灯类添加人接口
/// </summary>
public interface ITrafficLight
{
void AddPeople(IPeople people);
}
#endregion
#region 人类
/// <summary>
/// 人类
/// </summary>
public class People : IPeople
{
//IPeople 成员
private string name;
public People(string name, ITrafficLight trl)
{
this.name = name;
trl.AddPeople(this);
}
/// <summary>
/// 显示
/// </summary>
/// <param name="state">红绿灯状态</param>
public void Show(string state)
{
Console.WriteLine("红绿灯:" + name + ",我现在是" + state + ",别过斑马线!");
}
}
#endregion
#region 红绿灯类
/// <summary>
/// 红绿灯类
/// </summary>
public class TrafficLight : ITrafficLight
{
//保存观察者
private List<IPeople> people;
public TrafficLight()
{
this.people = new List<IPeople>();
}
public People People
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
/// <summary>
/// 添加人群
/// </summary>
/// <param name="peopleOther"></param>
public void AddPeople(IPeople peopleOther)
{
this.people.Add(peopleOther);
}
/// <summary>
/// 通知状态
/// </summary>
/// <param name="state">红绿灯状态</param>
public void ChangeState(string state)
{
Console.WriteLine("红绿灯状态变动");
foreach (IPeople peopleOther in this.people)
{
peopleOther.Show(state);
}
}
}
#endregion
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
People people1 = new People("毛毛", trl);
People people2 = new People("冉冉", trl);
People people3 = new People("娜娜", trl);
People people4 = new People("栋栋", trl);
trl.ChangeState("红灯");
Console.Read();
} }
设计体会:我们可以通过观察者模式把出生活中的很多场景归纳成模型,共开发使用。
3.验证阶段:
当前目标:增加一个人的特征维度,证明该模式的适用性;
代码浏览:
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
People people1 = new People("毛毛", trl);
People people2 = new People("冉冉", trl);
People people3 = new People("娜娜", trl);
People people4 = new People("栋栋", trl);
trl.ChangeState("红灯");
Console.Read();
}
验证结论:观察者模式适用于生活中的一对多且观察者通过观察到被观察者状态改变后观察者状态改变。
生活场景:我们在过马路的时候,总是会去观察红绿灯的状态。是红灯的时候,我们会停下脚步等待,变黄灯的时候我们就准备起步,绿灯我们就过斑马线了。
终极目标:实现自动发送红绿灯状态给每一个斑马线上的人。
1.不假思索的思路:通过类继承的方式来做上面的例子。即:先建立路类;然后派生出高速公路类、市区公路类;然后再高速公路类和市区公路类上分别派生出:小汽车类和公共汽车类。
类结构图:
代码浏览:
namespace Observer
{
/// <summary>
/// 人类
/// </summary>
public class People
{
private string name;
public string Name//字段封装
{
get { return name; }
set { name = value; }
}
public People(string name)
{
this.name = name;
}
/// <summary>
/// 查看红绿灯状态
/// </summary>
public void Watch()
{
Console.WriteLine(name + ":现在红绿灯是神马状态?");
}
}
/// <summary>
/// 红绿灯类
/// </summary>
public class TrafficLight
{
/// <summary>
/// 发送红绿灯状态
/// </summary>
/// <param name="state">红绿灯的状态</param>
public void SendMessage(string state)
{
People people = new People("阿毛");
people.Watch();
Console.WriteLine("红绿灯:"+people.Name + ",我现在是" + state + ",别过斑马线!");
}
}
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
trl.SendMessage("红灯");
Console.ReadKey();
}
}
}
存在问题:给不同的人发送状态,每次都要改变红绿灯类
2.归纳阶段
归纳步骤一
当前目标:实现多人群,且不改变红绿灯类。
思路:把人们的共有特点总结起来,以达到不改变红绿灯类。
类结构图:
代码浏览:
#region 接口声明
/// <summary>
/// 人类显示红绿灯接口
/// </summary>
public interface IPeople
{
void Show(string state);//显示红绿灯状态
}
/// <summary>
/// 红绿灯类添加人接口
/// </summary>
public interface ITrafficLight
{
void AddPeople(IPeople people);
}
#endregion
#region 人类
/// <summary>
/// 人类
/// </summary>
public class People : IPeople
{
//IPeople 成员
private string name;
public People(string name, ITrafficLight trl)
{
this.name = name;
trl.AddPeople(this);
}
/// <summary>
/// 显示
/// </summary>
/// <param name="state">红绿灯状态</param>
public void Show(string state)
{
Console.WriteLine("红绿灯:" + name + ",我现在是" + state + ",别过斑马线!");
}
}
#endregion
#region 红绿灯类
/// <summary>
/// 红绿灯类
/// </summary>
public class TrafficLight : ITrafficLight
{
//保存观察者
private List<IPeople> people;
public TrafficLight()
{
this.people = new List<IPeople>();
}
public People People
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
/// <summary>
/// 添加人群
/// </summary>
/// <param name="peopleOther"></param>
public void AddPeople(IPeople peopleOther)
{
this.people.Add(peopleOther);
}
/// <summary>
/// 通知状态
/// </summary>
/// <param name="state">红绿灯状态</param>
public void ChangeState(string state)
{
Console.WriteLine("红绿灯状态变动");
foreach (IPeople peopleOther in this.people)
{
peopleOther.Show(state);
}
}
}
#endregion
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
People people1 = new People("毛毛", trl);
People people2 = new People("冉冉", trl);
People people3 = new People("娜娜", trl);
People people4 = new People("栋栋", trl);
trl.ChangeState("红灯");
Console.Read();
} }
设计体会:我们可以通过观察者模式把出生活中的很多场景归纳成模型,共开发使用。
3.验证阶段:
当前目标:增加一个人的特征维度,证明该模式的适用性;
代码浏览:
class Program
{
static void Main(string[] args)
{
TrafficLight trl = new TrafficLight();
People people1 = new People("毛毛", trl);
People people2 = new People("冉冉", trl);
People people3 = new People("娜娜", trl);
People people4 = new People("栋栋", trl);
trl.ChangeState("红灯");
Console.Read();
}
验证结论:观察者模式适用于生活中的一对多且观察者通过观察到被观察者状态改变后观察者状态改变。
相关文章推荐
- 设计模式C++实现(15)——观察者模式
- 行为型模式-观察者模式的使用(一)
- Android开发----几大设计模式(单例模式、Builder模式、观察者模式、原型模式、策略模式)
- 设计模式之--观察者模式
- java设计模式-观察者模式
- 观察者模式(Observer Pattern)
- 设计模式:学习笔记(2)——观察者模式
- .NET设计模式(19):观察者模式(Observer Pattern)(转)
- Java设计模式补充:回调模式、事件监听器模式、观察者模式(转)
- 设计模式- 观察者模式
- Java观察者模式
- 设计者模式--观察者模式
- IOS设计模式之三(适配器模式,观察者模式)
- C++设计模式——观察者模式
- 设计模式:观察者模式
- 设计模式之观察者模式
- [非凡程序员]观察者模式
- C#设计模式——观察者模式(Observer Pattern)1
- Java设计模式02--观察者模式
- 行为型模式之观察者模式