首页 > 其他 > 详细

设计模式之观察者模式

时间:2016-11-24 14:54:24      阅读:195      评论:0      收藏:0      [点我收藏+]

1、类图

观察者模式结构:

技术分享

实例类图

技术分享

 

2、创建项目

…………………………

3、 AllyControlCenter:指挥部(战队控制中心)类,充当抽象目标类 

using System;

using System.Collections.Generic; 

namespace ObserverSample

{

    abstract class AllyControlCenter

    {

        protected string allyName; //战队名称

        protected List<IObserver> players = new List<IObserver>(); //定义一个集合用于存储战队成员

        public void SetAllyName(string allyName)

        {

            this.allyName = allyName;

        }

        public string GetAllyName()

        {

            return this.allyName;

        }

        //注册方法

        public void Join(IObserver obs)

        {

            Console.WriteLine("{0}加入{1}战队!", obs.Name, this.allyName);

    players.Add(obs);

    }

        //注销方法

        public void Quit(IObserver obs)

        {

            Console.WriteLine("{0}退出{1}战队!", obs.Name, this.allyName);

    players.Remove(obs);

    }

        //声明抽象通知方法

        public abstract void NotifyObserver(string name);

    }

}

4、 ConcreteAllyControlCenter:具体指挥部类,充当具体目标类

using System;

namespace ObserverSample

{

    class ConcreteAllyControlCenter : AllyControlCenter

    {

        public ConcreteAllyControlCenter(string allyName)

        {

            Console.WriteLine("{0}战队组建成功!", allyName);

            Console.WriteLine("----------------------------");

    this.allyName = allyName;

    }

        //实现通知方法

        public override void NotifyObserver(string name)

        {

            Console.WriteLine("{0}战队紧急通知,盟友{1}遭受敌人攻击!", this.allyName, name);

            //遍历观察者集合,调用每一个盟友(自己除外)的支援方法

            foreach(object obs in players)

            {

                if (!((IObserver)obs).Name.Equals(name))

                {

                    ((IObserver)obs).Help();

                }

            }

    }

    }

}

5、 IObserver:抽象观察者类

namespace ObserverSample

{

    interface IObserver

    {

        string Name

        {

            get;

            set;

        }

        void Help(); //声明支援盟友方法

        void BeAttacked(AllyControlCenter acc); //声明遭受攻击方法

    }

}

6、 Player:战队成员类,充当具体观察者类

using System;

namespace ObserverSample

{

    class Player : IObserver

    {

        private string name;

 

        public Player(string name)

        {

            this.name = name;

        }

        public string Name

        {

            get { return name; }

            set { name = value; }

        } 

        //支援盟友方法的实现

        public void Help()

        {

            Console.WriteLine("坚持住,{0}来救你!", this.name);

    } 

        //遭受攻击方法的实现,当遭受攻击时将调用战队控制中心类的通知方法NotifyObserver()来通知盟友

        public void BeAttacked(AllyControlCenter acc)

        {

            Console.WriteLine("{0}被攻击!", this.name);

            acc.NotifyObserver(name);

    }

    }

7、 Program:客户端测试类

using System; 

namespace ObserverSample

{

    class Program

    {

        static void Main(string[] args)

        {

            //定义观察目标对象

            AllyControlCenter acc;

            acc = new ConcreteAllyControlCenter("金庸群侠");

 

            //定义四个观察者对象

            IObserver player1, player2, player3, player4;

            player1 = new Player("杨过");

            acc.Join(player1); 

            player2 = new Player("令狐冲");

            acc.Join(player2);

            player3 = new Player("张无忌");

            acc.Join(player3);

            player4 = new Player("段誉");

            acc.Join(player4);

            //某成员遭受攻击

            player1.BeAttacked(acc);

            Console.Read();

        }

    }

}

8、 结果及分析,编译并运行程序,输出结果如下:

技术分享

 

设计模式之观察者模式

原文:http://www.cnblogs.com/cqxhl/p/6097423.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!