开发者

Publish subscriber pattern in c# Advice needed

开发者 https://www.devze.com 2023-03-06 19:22 出处:网络
I need to build something for a demo and and I was wondering if I can use the pub-sub pattern to implement a Console.Write and Console.ReadLine

I need to build something for a demo and and I was wondering if I can use the pub-sub pattern to implement a Console.Write and Console.ReadLine I want to print a set of questions and answers.and cannot find a way to implement the answer.

Where would you put your Console.Write() and your Console.ReadLine(); and pass it back

See my noddy example

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 Notif开发者_高级运维yObserversHandler(string question);

        public event NotifyObserversHandler QuestionChanged;

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

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

Any suggestions? thanks for your time


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]);
        }
    }
}

Not a best approach maybe. But it works for me. Sorry for the lack of description

Offtopic

yo dawg we heard you like observers, so we put an observer inside your subscriber, so you can observe when you observe

Edit

eventless subscriber:

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]);
    }
}


Not using events but derived classes ... http://protocolbus.casessite.org

Watch out with the environment variables (uses the forward slash instead of the windows )

0

精彩评论

暂无评论...
验证码 换一张
取 消