• 设计模式


    graph LR client--调用-->Handler ConcreteHandlerA--继承-->Handler ConcreteHandlerB--继承-->Handler Handler--引用-->Handler

    当 Handler 处理请求时,可以选择自己处理或调用继任者处理该请求。

    abstract class Handler
    {
        protected Handler _successor;
    
        public void SetSuccessor(Handler h)
        {
            this._successor = h;
        }
    
        public abstract void HandlerRequest(int request);
    }
    
    class ConcreteHandlerA : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request < 10)
            {
                Console.WriteLine(string.Format("{0} handle thing which is {1}", this.GetType().Name, request));
            }
            else if (_successor != null)
            {
                Console.WriteLine(string.Format("{0} cannot handle thing which is {1}", this.GetType().Name, request));
                _successor.HandlerRequest(request);
            }
        }
    }
    
    class ConcreteHandlerB : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request < 20)
            {
                Console.WriteLine(string.Format("{0} handle thing which is {1}", this.GetType().Name, request));
            }
            else if (_successor != null)
            {
                Console.WriteLine(string.Format("{0} cannot handle thing which is {1}", this.GetType().Name, request));
                _successor.HandlerRequest(request);
            }
        }
    }
    
    class ConcreteHandlerC : Handler
    {
        public override void HandlerRequest(int request)
        {
            if (request < 30)
            {
                Console.WriteLine(string.Format("{0} handler thing which is {1}", this.GetType().Name, request));
            }
            else
            {
                Console.WriteLine(string.Format("{0} say {1} 太高了,再说吧", this.GetType().Name, request));
            }
        }
    }
    
    class Boy : Handler
    {
        public override void HandlerRequest(int request)
        {
            _successor.HandlerRequest(request);
        }
    }
    
    
    // 业务代码:
    ConcreteHandlerC c = new ConcreteHandlerC();
    oncreteHandlerB b = new ConcreteHandlerB();
    b.SetSuccessor(c);
    
    ConcreteHandlerA a = new ConcreteHandlerA();
    a.SetSuccessor(b);
    
    int[] requests = {5, 15, 25, 35};
    foreach (int res in requests)
    {
        boy.HandlerRequest(res);
    }
    
    output:
    ConcreteHandlerA handle thing which is 5
    
    ConcreteHandlerA cannot handle thing which is 15
    ConcreteHandlerB handle thing which is 15
    
    ConcreteHandlerA cannot handle thing which is 25
    ConcreteHandlerB cannot handle thing which is 25
    ConcreteHandlerC handler thing which is 25
    
    ConcreteHandlerA cannot handle thing which is 35
    ConcreteHandlerB cannot handle thing which is 35
    ConcreteHandlerC say 35 太高了,再说吧
    
  • 相关阅读:
    知乎神回复:代码之间为什么要加空格?这个问题我是这样理解的!
    经验分享:一个 30 岁的人是如何转行做程序员,进入IT行业的?
    对于程序员来说,学历真的重要吗?为何都是高学历混的风生水起?
    教材、教参、教案有哪些区别?
    教参是什么
    教师面试指要
    教师资格证结构化面试是什么?会怎么考查?
    教师资格证面试试讲时可以戴手表吗
    讲师面试流程及试讲指导
    教师资格面试:试讲和说课的区别
  • 原文地址:https://www.cnblogs.com/MichaelLoveSna/p/14199171.html
Copyright © 2020-2023  润新知