示例#1
0
        public static void Main(string[] args)
        {
            //Instantiate publishers, subscribers and PubSubService
            IPublisher sportNewsPublisher     = new NewsPublisher();
            IPublisher politicalNewsPublisher = new NewsPublisher();

            Subscriber sportSubscriber    = new NewsSubscriber();
            Subscriber politicsSubscriber = new NewsSubscriber();
            Subscriber newsSubscriber     = new NewsSubscriber();


            //Declare Messages and Publish Messages to PubSubService
            Message sportMsg1    = new Message("Sport", "Sport news 1");
            Message sportMsg2    = new Message("Sport", "sport news 2");
            Message politicsMsg3 = new Message("Politics", "politics 1223");

            //
            PubSubService pubSubService = new PubSubService();

            sportNewsPublisher.publish(sportMsg1, pubSubService);
            sportNewsPublisher.publish(sportMsg2, pubSubService);
            politicalNewsPublisher.publish(politicsMsg3, pubSubService);



            //Declare Subscribers
            sportSubscriber.AddSubscriber("Sport", pubSubService);
            politicsSubscriber.AddSubscriber("Politics", pubSubService);
            newsSubscriber.AddSubscriber("Sport", pubSubService);
            newsSubscriber.AddSubscriber("Politics", pubSubService);

            //Trying unSubscribing a subscriber
            //newsSubscriber.unSubscribe("Sport", pubSubService);

            //Broadcast message to all subscribers. After broadcast, messageQueue will be empty in PubSubService
            pubSubService.Broadcast();

            //Print messages of each subscriber to see which messages they got
            Console.WriteLine("Messages of Sport Subscriber are: ");
            sportSubscriber.printMessages();

            Console.WriteLine("\nMessages of Politics Subscriber are: ");
            politicsSubscriber.printMessages();

            Console.WriteLine("\nMessages of News Subscriber are: ");
            newsSubscriber.printMessages();

            Console.Write("\nPress Any Key:");
            Console.ReadKey();
        }
示例#2
0
 public void publish(Message Message, PubSubService PubSubService)
 {
     PubSubService.AddMessageToQueue(Message);
 }
示例#3
0
 //Request specifically for messages related to topic from PubSubService
 public abstract void GetMessagesForSubscriberOfTopic(string Topic, PubSubService pubSubService);
示例#4
0
 //Unsubscribe subscriber with PubSubService for a topic
 public abstract void UnSubscribe(string Topic, PubSubService PubSubService);
示例#5
0
 //Add subscriber with PubSubService for a topic
 public abstract void AddSubscriber(string Topic, PubSubService PubSubService);
示例#6
0
 //Unsubscribe subscriber with PubSubService for a topic
 public override void UnSubscribe(string Topic, PubSubService PubSubService)
 {
     PubSubService.RemoveSubscriber(Topic, this);
 }
示例#7
0
 //Request specifically for messages related to topic from PubSubService
 public override void GetMessagesForSubscriberOfTopic(string Topic, PubSubService PubSubService)
 {
     PubSubService.GetMessagesForSubscriberOfTopic(Topic, this);
 }
示例#8
0
 //Add subscriber with PubSubService for a topic
 public override void AddSubscriber(string Topic, PubSubService PubSubService)
 {
     PubSubService.AddSubscriber(Topic, this);
 }