C# 委托责任链
为了我的理解目的,我实现了责任链模式。
//Abstract Base Type
public abstract class CustomerServiceDesk
{
protected CustomerServiceDesk _nextHandler;
public abstract void ServeCustomers(Customer _customer);
public void SetupHadler(CustomerServiceDesk _nextHandler)
{
this._nextHandler = _nextHandler;
}
}
public class FrontLineServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.General)
{
Console.WriteLine(_customer.Name + " Complaints are registered ;
will be served soon by FrontLine Help Desk..");
}
else
{
Console.WriteLine(_customer.Name + "
is redirected to Critical Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class CriticalIssueServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Critical)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ; will be served soon
by Critical Help Desk");
}
else if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"is redirected to Legal Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class LegalissueServiceDesk :CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ;
will be served soon by legal help desk");
}
}
}
public class Customer
{
public string Name { get; set; }
public ComplaintType ComplaintType { get; set; }
}
public enum ComplaintType
{
General,
Critical,
Legal
}
void Main()
{
CustomerServiceDesk _frontLineDesk = new FrontLineServiceDesk();
CustomerServiceDesk _criticalSupportDesk = new CriticalIssueServiceDesk();
CustomerServiceDesk _legalSupportDesk = new LegalissueServiceDesk();
_frontLineDesk.SetupHadler(_criticalSupportDesk);
_criticalSupportDesk.SetupHadler(_legalSupportDesk);
Customer _customer1 = new Customer();
_customer1.Name = "Microsoft";
_customer1.ComplaintType = ComplaintType.General;
Customer _customer2 = new Customer();
_customer2.Name = "SunSystems";
_customer2.ComplaintType = ComplaintType.Critical;
Customer _customer3 = new Customer();
_customer3.Name = "HP";
_customer3.ComplaintType = ComplaintType.Legal;
_frontLineDesk.ServeCustomers(_customer1);
_frontLineDesk.ServeCustomers(_customer2);
_frontLineDesk.ServeCustomers(_customer3);
}
问题
在不破坏责任链的情况下,我如何应用委托和事件来重写代码?
For my understanding purpose i have implemented Chain-Of-Responsibility pattern.
//Abstract Base Type
public abstract class CustomerServiceDesk
{
protected CustomerServiceDesk _nextHandler;
public abstract void ServeCustomers(Customer _customer);
public void SetupHadler(CustomerServiceDesk _nextHandler)
{
this._nextHandler = _nextHandler;
}
}
public class FrontLineServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.General)
{
Console.WriteLine(_customer.Name + " Complaints are registered ;
will be served soon by FrontLine Help Desk..");
}
else
{
Console.WriteLine(_customer.Name + "
is redirected to Critical Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class CriticalIssueServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Critical)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ; will be served soon
by Critical Help Desk");
}
else if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"is redirected to Legal Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class LegalissueServiceDesk :CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ;
will be served soon by legal help desk");
}
}
}
public class Customer
{
public string Name { get; set; }
public ComplaintType ComplaintType { get; set; }
}
public enum ComplaintType
{
General,
Critical,
Legal
}
void Main()
{
CustomerServiceDesk _frontLineDesk = new FrontLineServiceDesk();
CustomerServiceDesk _criticalSupportDesk = new CriticalIssueServiceDesk();
CustomerServiceDesk _legalSupportDesk = new LegalissueServiceDesk();
_frontLineDesk.SetupHadler(_criticalSupportDesk);
_criticalSupportDesk.SetupHadler(_legalSupportDesk);
Customer _customer1 = new Customer();
_customer1.Name = "Microsoft";
_customer1.ComplaintType = ComplaintType.General;
Customer _customer2 = new Customer();
_customer2.Name = "SunSystems";
_customer2.ComplaintType = ComplaintType.Critical;
Customer _customer3 = new Customer();
_customer3.Name = "HP";
_customer3.ComplaintType = ComplaintType.Legal;
_frontLineDesk.ServeCustomers(_customer1);
_frontLineDesk.ServeCustomers(_customer2);
_frontLineDesk.ServeCustomers(_customer3);
}
Question
Without breaking the chain-of-responsibility ,how can i apply delegates and events to rewrite the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果我理解正确的话...您可以做的是删除
SetupHandler
方法并引入OnElevateQuery
事件,如果您的CriticalHelpDesk
对象可以处理FrontLineHelpDesk.OnElevateQuery
事件和您的LegalHelpDesk
对象可以处理CriticalHelpDesk.OnElevateQuery
事件。OnElevateQuery
事件可以在事件参数中传递客户。示例
用法
但是,由于查询类型基于枚举
ComplaintType
,您是否考虑过使用HelpDeskFactory
可以返回一个通用接口,例如IHelpDesk
。听起来您也可以在这个特定示例中使用策略模式。If I understand you correctly...what you could do is remove the
SetupHandler
method and introduce anOnElevateQuery
event were yourCriticalHelpDesk
object could handle theFrontLineHelpDesk.OnElevateQuery
event and yourLegalHelpDesk
object could handle theCriticalHelpDesk.OnElevateQuery
event. TheOnElevateQuery
event could pass the customer in the event args.Example
Usage
However, as the query type is based on the enum
ComplaintType
have you considered using perhaps aHelpDeskFactory
which could return a generic interface e.g.IHelpDesk
. Sounds like you could also use the Strategy Pattern for this particular example.具有投诉类型的客户看起来像是一个放错位置的属性。我假设你的意思是投诉有一个类型。
我可能是错的,在这种情况下你可以指出缺少什么行为
对我来说这只是一个事件。每个事件处理程序将按订阅顺序调用。每个处理程序都可以根据投诉忽略通知。只要 eventArgs 的 Handled 属性为 false 并且存在挂起的订阅者,就会调用下一个处理程序。
Customer having a complaintType looks like a misplaced attribute. I assume you mean a Complaint has a Type.
I may be wrong in which case you can point what behavior is missing
This just looks like an event to me. Each event handler would be called in order of subscription. Each handler is free to ignore the notification based on the complaint. The next handler is called as long as the Handled property of the eventArgs is false and there are pending subscribers.
这与上面的答案非常相似,但更精简。 :)
This is very similar to above answers, but more streamlined. :)