zoukankan      html  css  js  c++  java
  • 利用RealProxy,强化AOP,在”牛粪“的周围插上”鲜花“


    有个接口

        interface I牛粪
        {
            
    void 吃();
        }

    有个接口的实现

        class 牛粪 : I牛粪
        {
            
    public void 吃()
            {
                Console.WriteLine(
    "吃 牛粪 拉!");
            }
        }

    我现在需要在这个吃的动作周围加上一大堆的鲜花,还有围观的群众。如果使用传统的编程方法,得到:

    代码
            public void Test001()
            {
                牛粪 sheet 
    = new 牛粪();

                
    for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(
    "鲜花");
                }

                Console.WriteLine(
    "围观群众");

                sheet.吃();

                Console.WriteLine(
    "围观群众");

                
    for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(
    "鲜花");
                }
            }

    如果要还有添加”喝 “,“拉”之类的,怎么办?如果不同动作的入口参数也不一样,怎么办?重复的方法不能写很多次。于是使用代理基础。

    代码
        class Mock<T> : RealProxy
        {
            T instance;
            
    public delegate void OnMock(IMockMethod method);
            
    public event OnMock OnMockMethod;
            
    class MockMethod : IMockMethod
            {
                T instance;
                IMethodCallMessage methodCall;
                
    object result;
                
    public MockMethod(T instance, IMethodCallMessage methodCall)
                {
                    
    this.instance = instance;
                    
    this.methodCall = methodCall;
                }
                
    public void Execute()
                {
                    result 
    = methodCall.MethodBase.Invoke(instance, methodCall.InArgs);
                }
                
    public object Result
                {
                    
    get
                    {
                        
    return result;
                    }
                }
            }

            
    public Mock(T instance)
                : 
    base(typeof(T))
            {
                
    this.instance = instance;
            }

            
    public override IMessage Invoke(IMessage msg)
            {
                IMethodCallMessage methodCall 
    = msg as IMethodCallMessage;

                MockMethod mock 
    = new MockMethod(this.instance, methodCall);

                
    this.OnMockMethod(mock);

                
    return new ReturnMessage(mock.Result, null0null, methodCall);
            }
            
    public T Value
            {
                
    get
                {
                    
    return (T)this.GetTransparentProxy();
                }
            }
            
    public bool Result
            {
                
    get
                {
                    
    return false;
                }
            }
        }

        
    interface IMockMethod
        {
            
    void Execute();
        }

    最后调用是:

    代码
            public void test222()
            {
                Mock
    <I牛粪> mock = new Mock<I牛粪>(new 牛粪());
                mock.OnMockMethod 
    += new Mock<I牛粪>.OnMock(mock_OnMockMethod);
                mock.Value.吃();
            }

            
    void mock_OnMockMethod(IMockMethod method)
            {
                
    for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(
    "鲜花");
                }
                Console.WriteLine(
    "围观群众");
                method.Execute();
                Console.WriteLine(
    "围观群众");

                
    for (int i = 0; i < 3; i++)
                {
                    Console.WriteLine(
    "鲜花");
                }
            }

    不知道这个技术算不算新。 

  • 相关阅读:
    ATM+购物车
    subprocess,re,logging模块
    json,pickle,collections,openpyxl模块
    time,datatime,random,os,sys,hashlib模块
    1.内置函数剩余部分 map reduce filter 2.函数递归 3.模块
    生成器,面向过程编程,三元表达式,列表生成式,生成器表达式,匿名函数,内置函数
    Ajax数据对接出问题了?ThingJS解决方法在这里
    测试
    简单低成本的物联网开发平台-ThingJS
    用ThingJS之CityBuilder快搭3D场景,可视化开发必备
  • 原文地址:https://www.cnblogs.com/zc22/p/1677451.html
Copyright © 2011-2022 走看看