class 的私有方法装饰模式

Patterns for decorating private methods of a class

在下面 class 我有一个 public 方法叫做 ProcessMessage。此方法负责处理传入的消息。处理消息涉及不同的阶段。我想装饰这个 class 以便我可以发布来自消息处理的每个阶段的性能计数器值。

我知道我可以覆盖 ProcessMessage 方法并通过发布性能计数器值再次重写逻辑。但是有没有我可以应用的更好的方法/模式,这样我就不必在装饰的 class.

中再次复制逻辑
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 对象列表而不是一堆方法。通过这种方式,您可以 add/skip/change 呼叫订单。在您的 IProcessor 中声明 Process(PerformanceContext context) 方法并实现 PerformanceContext class 以交换一些值,如 StartTime、NumberOfCalls 等

祝你好运!