Var [Design Mode] chain of responsibility pattern of - responsibility chain

Today saw the chain of responsibility pattern wikipedia, under the curious search for related articles, this article is mainly

91 large [.NET] Reconstruction of the road series v11 & ndash; break the annoying switch case with the chain of responsibility pattern

Variants practice, so the original design ideas with the chain of responsibility pattern is not the same, but the same result,

So before reading, I recommended reading after the first 91 large articles, in view of this it would be more able to feel the difference.


 Here the first reference to the complementary 91 large end of the article to review the conclusions of the focus of the chain of responsibility,

20,120,415 supplement: the chain of responsibility is intended to divide the responsibilities of each role clear, each object only needs to do their own thing, the way through the chain of responsibility be combined, and complete usage scenario needs. Responsibilities of the role are as follows:

  1. Each object on the chain of responsibility, just know 'what for, on behalf of their own responsibility', and only know 'their duties, should be doing'.
  2. Responsibility abstract objects, only know 'passed down' about it.
  3. Use the scene, only know 'calls this process the chain of responsibility', you can get the desired results.


responsibility

Point 2 is what I want to change the part, if the system really need to use a lot of local party liability chain mode,

I'll see a bunch of abstract objects responsibility to do this thing responsible for passing down,

So I hope the responsibility of each object is just to do what they ought to do, there is not enough of their responsibilities,

So each object has no responsibility to inherit the abstract object pass the responsibility of the relevant code down:

        public interface IMonkey
        {
            bool DoSomething(DayOfWeek today);
        }

        public abstract class AbstractMonkey : IMonkey
        {
            public virtual bool DoSomething(DayOfWeek today)
            {
                // 是自己的责任,就做
                var result = IsMyResponsibility(today);
                if (result)
                    MyAction(today);
                return result;
            }

            protected abstract void MyAction(DayOfWeek today);
            protected abstract bool IsMyResponsibility(DayOfWeek today);
        }

        public class MonkeyMonday : AbstractMonkey
        {
            protected override void MyAction(DayOfWeek today)
            {
                Console.WriteLine("星期一,猴子穿新衣");
            }

            protected override bool IsMyResponsibility(DayOfWeek today)
            {
                return today == DayOfWeek.Monday;
            }
        }

        public class MonkeyTuesday : AbstractMonkey
        {
            protected override void MyAction(DayOfWeek today)
            {
                Console.WriteLine("星期二,猴子肚子饿");
            }

            protected override bool IsMyResponsibility(DayOfWeek today)
            {
                return today == DayOfWeek.Tuesday;
            }
        }

        public class MonkeySunday : AbstractMonkey
        {
            protected override void MyAction(DayOfWeek today)
            {
                Console.WriteLine("星期日,猴子过生日");
            }

            protected override bool IsMyResponsibility(DayOfWeek today)
            {
                return today == DayOfWeek.Sunday;
            }
        }

Seen from above AbstractMonkey just simply use the template method, requiring the monkeys practice must practice MyAction and IsMyResponsibility,

The process AbstractMonkey practice IMonkey.DoSomething do certain things, and return if there is to do.


chain

Above that code, only the responsibility of the object should do something, that is, we have the responsibility, and that then I want to be a chain:

        public class Chain
  
  
   
   
        {
            private IList
   
   
    
     _chain = new List
    
    
     
     ();

            public Chain
     
     
      
       SetNext(T item)
            {
                _chain.Add(item);
                return this;
            }

            public void Execute(Func
      
      
        doSomething) { foreach (var item in _chain) { if (doSomething(item)) break; } } } 
      
     
     
    
    
   
   
  
  

 Chain intention is to 'pass down', so there SetNext responsibility to set the next object, and then use Execute delegate parameter to check whether a particular object return stop doing.


Use

        static void Main(string[] args)
        {
            var today = DateTime.Today.DayOfWeek;
            var monkeyChain = new Chain
  
  
   
   ()
                .SetNext(new MonkeyMonday())
                .SetNext(new MonkeyTuesday())
                .SetNext(new MonkeySunday());

            monkeyChain.Execute(monkey => monkey.DoSomething(today));
        }
  
  

First with SetNext will IMonkey practice strung together, the responsibility for implementation through IMonkey.DoSomething chain.

Results of the


summary

This is a variant of the chain of responsibility, with responsibility split into major chains,

In order to use the chain of responsibility, so that the chain of responsibility to write no more burden,

The following is the chain after repeated use can be reduced code (91 references a large part of the code as an example):

1. Reduce the use of write constructor (or method) is set a code for the next object responsibility.

        public AbstractMonkey(AbstractMonkey monkey)
        {
            this._nextMonkey = monkey;
        }

2. Reduce the responsibility to write stored at a private field object field code.

private AbstractMonkey _nextMonkey;

3. Reduce how to write code execution under a responsibility object.

        public void DoSomething(DayOfWeek today)
        {
            if (isMyResponsibility(today))
            {
                this.MyAction(today);
            }
            else
            {
                if (this._nextMonkey != null)
                {
                    this._nextMonkey.DoSomething(today);
                }
                else
                {
                    Console.WriteLine("责任链接束!");
                }

            }
        }


Reference article

[.NET] Reconstruction of the road series v11 & ndash; break the annoying switch case with the chain of responsibility pattern

Design pattern (17) -Chain of Responsibility Pattern

Original: Large column  variant [Design Mode] chain of responsibility pattern of - responsibility chain


Guess you like

Origin www.cnblogs.com/chinatrump/p/11505160.html