C# 装饰类的私有方法的模式

C# 装饰类的私有方法的模式,c#,design-patterns,decorator,C#,Design Patterns,Decorator,在下面的类中,我有一个名为ProcessMessage的公共方法。此方法负责处理传入消息。处理消息涉及不同的阶段。我想用这样一种方式来装饰这个类,即我可以发布消息处理每个阶段的性能计数器值 我知道我可以重写ProcessMessage方法,并使用发布性能计数器值再次重写逻辑。但是有没有更好的方法/模式可以应用,这样我就不必在修饰类中再次复制逻辑了 public class MessageProcessor { public void ProcessMessage() {

在下面的类中,我有一个名为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

    }

}

谢谢。

使用IProcessor对象列表,而不是一堆方法。通过这种方式,您可以添加/跳过/更改呼叫顺序。在IPProcessor中,声明Process(PerformanceContext上下文)方法并实现PerformanceContext类以交换一些值,如StartTime、NumberOfCalls等


祝你好运

什么是性能计数器特性?为什么不使用composition,而只使用MessageProcessor类呢?然后只从新类调用ProcessMessage()方法?您的装饰程序是只调用
MessageProcessor.ProcessMessage
还是从
MessageProcessor
继承而来?在覆盖每个方法的情况下,是否可以不保护这些方法,执行性能计算并调用基类方法?不是理想的解决方案,但可能是一个选项。我的装饰程序继承自MessageProcessor。我没有MessageProcessor类的源代码来更改实现。谢谢Denis,我要求我的供应商按照您建议的方式提供该类。我还根据你的建议做了一个小的POC,效果很好。