我试图通过在Servicer类字符串中搜索字符/字符串来演示责任链模式的使用。该代码运行,但Servicer1 ServiceReq没有调用Servicer2 ServiceReq。如果我用“ g”运行它,则只会得到“ Checked Servicer1'”,但我应该得到“ Checked Servicer1'”“在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";
        }
    }


表格代码:

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

最佳答案

第一个问题是您永远不会对successor.ServiceReq的返回值执行任何操作。下一个问题是您似乎希望从ServiceReq返回多个消息。为此,您应该使返回类型为IEnumerable<string>,并对代码进行相应的调整。

这是调整“服务员”的方法:

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


这是对表单代码的更改:

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

            ...
        }
    }
}


只要您在文本框中输入了适当的字符,这将导致列表包含预期的输出。

关于c# - 责任链-Servicer1不致电Servicer2,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34275094/

10-13 07:29