Design Patterns ---- duty chain

        Today, we learn from duty chain, chain of responsibility pattern, literally meaning difficult to understand really, we want to understand the next split, responsibilities and chain of responsibility without explanation is that they need to do, then chain it, what is the chain, the chain is the thing! We all know that the chains generally composed of a circle of a ring consisting of, hey, of a ring, the ring is not that our software world objects? Even then it means that the duty like a chain of a ring connected together, it would constitute what we call the chain of responsibility pattern, then why is there duty chain it? This is not to decouple Well, for the thing decoupling, not just to reuse Well, for the thing multiplexing, not to be lazy thing, so blame them, chain of responsibility pattern is to be lazy (ha ha), the next time , let's take a closer talk about how lazy.

        I do not know if you have written a very complex business logic of it? It is not written numerous if and else if and else plus numerous it? Although there is a special sense of fun after the last hearty written, however, a test, you will find that your logic wrong home, and then look back to write their own code, FML, which wrote what , layers of nested if else to see people scalp tingling, today, our program chain of responsibility, though not completely solve this problem, but the chain of responsibility can teach us thinking that is devoid of judgment. Well, ado, we get into today's topic, chain of responsibility, what should we abstract it? Responsibility? ? Circle of a chain, no, these are not enough, we should be abstracted as a responsibility of the objects, see the code

abstract class Handler
    {
        protected Handler sueecssor;
        public void SetSuccerssor(Handler sueecssor)
        {
            this.sueecssor = sueecssor;
        }
        public abstract void HandleRequest(int request);
    }

Handler class of abstract objects that we want today as a duty, first of all we see its interior defines a type of sueecssor own property, note that this property is not private, so the aim is for what? In fact, just to make up our chain, this is our duty link properties, specifically how to link, we look at the following code

 class ConcreteHandler1 : Handler
    {
        public override void HandleRequest(int request)
        {
            if (request >= 0 && request < 10)
            {
                Console.WriteLine("{0} 处理请求{1}",
                this.GetType().Name, request);
            }
            else if (sueecssor != null) sueecssor.HandleRequest(request);

        }
    }

        This ConcreteHandler1 class, we achieved Handler abstract class overrides the method Handlerequest abstract class, we carefully look inside the method, if the argument does not satisfy the conditions of the incoming, then taught to control our next handler, how to pay It is through we just abstract class sueecssor property, may still not well understood, so that we are called to understand by the client

class Program
    {
        static void Main(string[] args)
        {
            ConcreteHandler1 concrete0 = new ConcreteHandler1();
            ConcreteHandler1 concrete1 = new ConcreteHandler1();
            ConcreteHandler1 concrete2 = new ConcreteHandler1();
            concrete0.HandleRequest(9);
            concrete0.SetSuccerssor(concrete1);
            concrete1.SetSuccerssor(concrete2);
            Console.Read();
        }
    }

       We ConcreteHandler1 declares three instances, namely concrete0, concrete1, concrete2, first of all, we will start with 0 to determine the responsibilities of the class, and then add concrete1 to judge, and then followed by concrete2 to judge, this is not like a chain, like a buckle part of it? Then we say why it instantiated subclass the parent class can call it? First, we instantiate ConcreteHandler1 class, because it is inherited from Handler class, it will first go to Handle create an abstract class, you can create a subclass, so that's why subclasses of the reasons the parent class method can be used, so, before declaration of sueecssor property is to be able to call only allowed in the subclass was declared as protected, here, our duty chain is complete

Guess you like

Origin www.cnblogs.com/liuhuimh/p/10964197.html