zoukankan      html  css  js  c++  java
  • 结合项目实例 回顾传统设计模式(二)观察者模式

    观察者模式现在用的不是很多 重点看下它的设计思想 OK 下面继续 消息中心的那点事

    /// <summary>
        
    /// 数据中心
        
    /// </summary>
        public class MessageData
        {
            
            public void messageSend()
            {
                string title = observer.getTitle();
                string content = observer.getContent();

                //针对具体实现编程,会导致我们以后再增加或者删除消息平台时 必须要重新维护Platformlist 有时甚至还要重构代码
                foreach (Platform p in observer.Platformlist)
                {
                    p.update(title, content);
                }
            }
        }

    上述情况在soa体系架构中凸显比较严重,基本上数据中心作为基站程序不适合版本经常更新

    那么这和一对多得关系有何关联

    利用观察者模式,数据中心是具体状态的对象,并且可以控制这些状态。也就是说,有“一个”具有状态的数据体。另一方面,观察者模式使用这些状态,虽然这些状态不属于他们。有许多的观察者,依赖主题来告诉他们状态合适改变了。这就产生了一个关系:“一个”数据中心对于“多个”消息发布者的关系。

    松耦合的威力:当两个对象松耦合,他们依然可以交互,但是不清楚彼此的细节。观察者模式提供了一种对象设计,让主题和观察者之间松耦合。

    /// <summary>
        
    /// 数据中心
        
    /// </summary>
        public class MessageData : MessageDateIF
        {
            private ArrayList observers;
            private string title;
            private string content;

            public MessageData()
            {
                observers = new ArrayList();
            }

            public void RegisterPlatform(observerIF o)
            {
                observers.Add(o);
            }

            public void RemovePlatform(observerIF o)
            {
                int i = observers.IndexOf(o);
                if (i > 0) { observers.Remove(i); }
            }

            public void NotifyPlatform()
            {
                foreach (observerIF o in observers)
                {
                    o.update(title, content);
                }
            }
            
            public void MessageChanged(string title,string content)
            {
                this.title = title;
                this.content = content;
                NotifyPlatform();
            }
        }

        public interface observerIF
        {
            void update(string title, string content);
           
        }
       
        public interface MessageDateIF
        {
            void RegisterPlatform(observerIF o);
            void RemovePlatform(observerIF o);
            void NotifyPlatform();
        }

        public interface DisplayElemnet
        {
            void Display();
        }

        public class PlatformA : observerIF, DisplayElemnet
        {
            private string title;
            private string content;
            private MessageDateIF messagedata;

            public PlatformA(MessageDateIF messagedata)
            {
                this.messagedata = messagedata;
            }
            public void update(string title, string content)
            {
                this.title = title;
                this.content = content;
                Display();
            }
            public void Display()
            {
                Console.WriteLine("title:" + title + " content:" + content);
            }
        }

    总结:为了对象之间的松耦合而设计。观察者模式定义了对象之间一对多依赖,这样一来,当一个对象改变状态时,它的所有一拦着都会收到通知并自动更新

    原创作品允许转载,转载时请务必以超链接形式标明文章原始出处以及作者信息。
    作者:熬夜的虫子
    点击查看:博文索引
  • 相关阅读:
    codeforces 980A Links and Pearls
    zoj 3640 Help Me Escape
    sgu 495 Kids and Prizes
    poj 3071 Football
    hdu 3853 LOOPS
    hdu 4035 Maze
    hdu 4405 Aeroplane chess
    poj 2096 Collecting Bugs
    scu 4444 Travel
    zoj 3870 Team Formation
  • 原文地址:https://www.cnblogs.com/dubing/p/2197978.html
Copyright © 2011-2022 走看看