C # responsibility chain model

Source: Internet
Author: User

The construction of "Iron" has never been stopped. Currently, a single part is available, but before components are assembled from the factory, it is still very necessary for us to conduct quality inspection on components, or inspect other aspects, or set the part identification information. These operations can be sequential (or unordered ).

Now we want to implement the functions mentioned above for demonstration. However, problems will be found during the process and then solved. The topic is directly entered.

First, I defined a ComponentModel class, which is the object to be tested.

                                   Name { ;                                                   }

Here we first define an enumeration of request processing types, which will be used below, which is much better than what strings or numbers are used for condition determination.

                                                                                         }

Then, let's define the type of the ComponentModel class:

                                                    _comModel =                                                                 (_comModel.Value >                                                                                   _comModel.Name =                                                                                                                                             _comModel =                                                                 (_comModel.Value >                                                                                       _comModel.Name =                                                                                          }

Two types are defined, ConcreteHandlerCaseOne and ConcreteHandlerCaseTwo, which are used to process and detect the ComponentModel type. Now these types are complete. Let's check them.

 ComponentModel comModel =  ConcreteHandlerCaseOne caseone =   ConcreteHandlerCaseTwo casetwo =  casetwo.HandleRequest(RequestState.Check);

Yes, that's it. If you want to detect 20 times and perform different implementations, it will be terrible and code redundancy, in addition, the coupling between the request caller and the handler is also very large. How to Make the code more streamlined and effectively decouple the code is necessary here the responsibility chain model is used.

 

To avoid the coupling between the request sender and receiver, multiple receiver objects have the opportunity to process the request. Connect these objects into a chain and pass the request along the chain until an object processes it.
-- Gof

It should be emphasized that the example in this article does not fully comply with the definition of the design pattern, or it is designed according to the functional requirements at the beginning of this article. Of course, the core of the pattern remains unchanged.

Abstract The Handler first:

                                                         .Successor =                  }

Now that there is an abstraction, we have to have a specific implementation:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                               }

The type here should be defined only to make everyone better understand.

Here we look at the Handle type of the abstract processor. There is a protected-level variable Successor. What about Successor represents the pointer of every link in the chain table? Of course, it points to the next processor.
Now let's take a look at the called code:

 ComponentModel comModel =  Handle handlerA =  Handle handlerB =   handlerA.HandleRequest(RequestState.Check, comModel);

It looks good, and the coupling degree is still very large. For handlerA calls, the caller still calls them directly. In this case, an intermediate layer is required,

Let's take a look at the definition of the intermediate layer:

                                                                 : (              _ComModel =                        _Handle =             _ComModel =                          (_Handle !=                   _Handle.SetSuccessor(handle);                                _Handle =                    }

After adding a layer, let's take a look at the caller's code:

 ComponentModel comModel =  CORUnit corunit =  corunit.RegisterHandle( corunit.RegisterHandle(   

Do you feel that the relationship between the caller and the actual handler has become weak, so that the goal is achieved.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.