您的位置:首页 > 编程语言 > C#

观察者模式

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();

        }

验证结论:观察者模式适用于生活中的一对多且观察者通过观察到被观察者状态改变后观察者状态改变。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息