2

I am trying to demonstrate use of chain of responsibility pattern by searching characters/ string inside Servicer class strings. The code runs but Servicer1 ServiceReq is not calling Servicer2 ServiceReq. If I run this with "g", I get"Checked Servicer1'" only but I should get "Checked Servicer1'" "Request found in Servicer 2".

MainApp.cs
namespace ChainOfResponsibility
{
    abstract class Servicer
    {
        protected Servicer successor;

        public void SetSuccessor( Servicer s)
        {
            successor = s;
        }
        public abstract string ServiceReq(char request);
    }

    class Servicer1 : Servicer
    {
        public override string ServiceReq(char request)
        {
            string s1 = "Sam ate nuts";
            if (s1.Contains(request))
            {
                return "Request found in Servicer 1";
            }
            else if (successor != null)
            {
                successor.ServiceReq(request);
            }
            return "Checked Servicer1'";
        }
    }

    class Servicer2 : Servicer
    {
        public override string ServiceReq(char request)
        {
            string s2 = "Apples are great";
            if (s2.Contains(request))
            {
                return "Request found in Servicer 2";
            }
            else if (successor != null)
            {
                successor.ServiceReq(request);
            }
            return "Checked Servicer 2";
        }
    }

Form Code:

namespace ChainOfResponsibility
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            char request = System.Convert.ToChar(textBox1.Text);
            Servicer h1 = new Servicer1();
            Servicer h2 = new Servicer2();
            h1.SetSuccessor(h2);

            AddToList(h1.ServiceReq(request));
        }
        private void AddToList(string message)
        {
            listBox1.Items.Add(message);

            listBox1.Items.Add("----------------------------------------");

            if (listBox1.Items.Count > 0)
                listBox1.TopIndex = listBox1.Items.Count - 1;
        }
    }
}
Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
kunal279
  • 31
  • 1
  • 4
  • Please post what you have tried and your expected outcome – RyanR Dec 14 '15 at 19:31
  • 1
    What is the input you're providing through the form? Also, your expected output can never happen; you call `successor.ServiceReq`, but never do anything with the return value. – Asad Saeeduddin Dec 14 '15 at 19:34
  • You call `successor.ServiceReq(request);` but do nothing with the value returned. – 001 Dec 14 '15 at 19:36

1 Answers1

0

The first problem is that you never do anything with the return value of successor.ServiceReq. The next problem is that you seem to want multiple messages returned from ServiceReq. To do this, you should make the return type IEnumerable<string>, and make corresponding adjustments to your code.

Here is how you would adjust your "servicers":

abstract class Servicer
{
    ...
    public abstract IEnumerable<string> ServiceReq(char request);
}

class Servicer1 : Servicer
{
    public override IEnumerable<string> ServiceReq(char request)
    {
        string s1 = "Sam ate nuts";
        yield return "Checked Servicer1";
        if (s1.Contains(request))
        {
            yield return "Request found in Servicer 1";
        }
        else if (successor != null)
        {
            yield return successor.ServiceReq(request);
        }
    }
}

// Similar changes for Servicer2

And here are the changes to your form code:

namespace ChainOfResponsibility
{
    public partial class Form1 : Form
    {
        ...
        private void AddToList(IEnumerable<string> messages)
        {
            listBox1.Items.AddRange(messages);

            ...
        }
    }
}

This results in the list containing your expected output, provided you've entered the appropriate character in the textbox.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139