【发布时间】:2015-06-01 12:12:34
【问题描述】:
在下面的类中,我有一个名为 ProcessMessage 的公共方法。此方法负责处理传入的消息。处理消息涉及不同的阶段。我想以这样一种方式装饰这个类,以便我可以从消息处理的每个阶段发布性能计数器值。
我知道我可以重写 ProcessMessage 方法并使用发布性能计数器值再次重写逻辑。但是有没有更好的方法/模式可以应用,这样我就不必在装饰类中再次复制逻辑。
public class MessageProcessor
{
public void ProcessMessage()
{
ConvertReceivedMessage();
SendToThirdParty();
ReceiveResponse();
ConvertResponseMessage();
SendResponseToClient();
}
private void ConvertReceivedMessage()
{
//here I want to publish the performance counter value from the decorated class
}
private void SendToThirdParty()
{
//here I want to publish the performance counter value from the decorated class
}
private void ReceiveResponse()
{
//here I want to publish the performance counter value from the decorated class
}
private void ConvertResponseMessage()
{
//here I want to publish the performance counter value from the decorated class
}
private void SendResponseToClient()
{
//here I want to publish the performance counter value from the decorated class
}
}
谢谢。
【问题讨论】:
-
什么是性能计数器功能?
-
为什么不使用组合,而只使用 MessageProcessor 类呢?然后只调用新类中的 ProcessMessage() 方法?
-
你的装饰器是直接调用
MessageProcessor.ProcessMessage还是继承自MessageProcessor -
可以不保护方法,每个方法都被覆盖,你的性能计算,调用基类方法吗?不是理想的解决方案,但可能是一种选择。
-
我的装饰器继承自 MessageProcessor 。我没有 MessageProcessor 类的源代码来更改实现。
标签: c# design-patterns decorator