责任链-服务1未调用服务2

本文关键字:服务 调用 责任 | 更新日期: 2023-09-27 18:26:49

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

责任链-服务1未调用服务2

第一个问题是永远不会对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);
            ...
        }
    }
}

如果在文本框中输入了适当的字符,则会生成包含预期输出的列表。