在c#中发布订阅模式需要建议

时间:2011-05-17 19:31:32

标签: c# design-patterns

我需要为演示构建一些东西,我想知道我是否可以使用pub-sub模式来实现Console.Write和Console.ReadLine 我想打印一组问题和答案。并且无法找到实现答案的方法。

你将Console.Write()和Console.ReadLine()放在哪里;并传回来

参见我的例子

namespace PubSubOne
{
    class Program
    {
        static void Main()
        {
            var processQuestions = new ProcessQuestions();
            processQuestions.StartQuestions();

            Console.Read();
        }
    }

    public class ProcessQuestions
    {
        NewsSubscriber subscriber = new NewsSubscriber();
        NewsPublisher publisher = new NewsPublisher();

        public ProcessQuestions()
        {

            publisher.QuestionChanged += subscriber.Update;


        }
        public void StartQuestions()
        {
            publisher.PublishQuestion("what is your favourite Newspaper?");

        }
    }
    public class NewsSubscriber
    {
        public void Update(string question)
        {
            Console.Write(question);
        }
    }
    public class NewsPublisher
    {
        private readonly List<string> _questions = new List<string>();

        public delegate void NotifyObserversHandler(string question);

        public event NotifyObserversHandler QuestionChanged;

        public void PublishQuestion(string question)
        {
            _questions.Add(question);

            QuestionChanged(_questions[_questions.Count - 1]);
        }
    }
}

有什么建议吗? 谢谢你的时间

2 个答案:

答案 0 :(得分:1)

using System;
using System.Collections.Generic;

namespace PubSubOne
{
    class Program
    {
        static void Main()
        {
            var processQuestions = new ProcessQuestions();
            processQuestions.StartQuestions();

            Console.ReadLine();
        }
    }

    public class ProcessQuestions
    {
        NewsSubscriber subscriber = new NewsSubscriber();
        NewsPublisher publisher = new NewsPublisher();

        Stack<string> _answers = new Stack<string>();

        public ProcessQuestions()
        {
            publisher.QuestionAsked += subscriber.Asked;
            subscriber.QuestionAnswered += (answer) => _answers.Push(answer);
        }


        public void StartQuestions()
        {
            publisher.PublishQuestion("what is your favourite Newspaper?");
            publisher.PublishQuestion("what is your email?");
            publisher.PublishQuestion("what is your email password?");

            Console.WriteLine("Answers: ");

            foreach (var answer in _answers)
            {
                Console.WriteLine(answer);
            }
        }
    }

    public class NewsSubscriber
    {
        public delegate void NotifyAnswered(string question);

        public event NotifyAnswered QuestionAnswered;

        public void Asked(string question)
        {
            Console.Write(question);

            if (QuestionAnswered != null)
            {
                QuestionAnswered(Console.ReadLine());
            }
        }
    }

    public class NewsPublisher
    {
        private readonly List<string> _questions = new List<string>();

        public delegate void NotifyObserversHandler(string question);

        public event NotifyObserversHandler QuestionAsked;

        public void PublishQuestion(string question)
        {
            _questions.Add(question);
            QuestionAsked(_questions[_questions.Count - 1]);
        }
    }
}

也许不是最好的方法。但它对我有用。很抱歉缺少说明

Offtopic

哟dawg我们听说你喜欢观察者,所以我们在你的订阅者中放了一个观察者,这样你就可以观察到什么时候观察

修改

无事件订阅者:

class Program
{
    static void Main()
    {
        var processQuestions = new ProcessQuestions();
        processQuestions.StartQuestions();
        processQuestions.PrintAnswers();
        Console.ReadLine();
    }
}

public class ProcessQuestions
{
    NewsSubscriber subscriber1 = new NewsSubscriber("Tim");
    NewsSubscriber subscriber2 = new NewsSubscriber("Bob");
    NewsPublisher publisher = new NewsPublisher();

    public ProcessQuestions()
    {
        publisher.QuestionAsked += subscriber1.Asked;
        publisher.QuestionAsked += subscriber2.Asked;
    }

    public void StartQuestions()
    {
        publisher.PublishQuestion("what is your favourite Newspaper?");
        publisher.PublishQuestion("what is your email?");
        publisher.PublishQuestion("what is your email password?");
    }

    public void PrintAnswers()
    {
        var subs = new[] {subscriber1, subscriber2};

        foreach (var newsSubscriber in subs)
        {
            Console.WriteLine(newsSubscriber.Name + " answers:");
            foreach (var answer in newsSubscriber.Answers)
            {
                Console.WriteLine(answer);
            }
        }
    }
}

public class NewsSubscriber
{
    private readonly string _name;

    public NewsSubscriber(string name)
    {
        _name = name;
    }

    List<string> _answers = new List<string>();

    public string Name
    {
        get { return _name; }
    }

    public List<string> Answers { get { return _answers; } }

    public void Asked(string question)
    {
        Console.WriteLine(_name + " says: ");
        _answers.Add(Console.ReadLine());
    }
}

public class NewsPublisher
{
    private readonly List<string> _questions = new List<string>();

    public delegate void NotifyObserversHandler(string question);

    public event NotifyObserversHandler QuestionAsked;

    public void PublishQuestion(string question)
    {
        _questions.Add(question);
        Console.Write(question);
        QuestionAsked(_questions[_questions.Count - 1]);
    }
}

答案 1 :(得分:0)

不使用事件而是派生类... http://protocolbus.casessite.org

注意环境变量(使用正斜杠而不是窗口)