Design Pattern 14-responsibility chain pattern of behavioral pattern

Source: Internet
Author: User

Definition: The Chain OfResponsibility Pattern provides an opportunity for all slave objects to process requests, thus avoiding coupling between request senders and receivers. Connect these objects into a chain and pass requests along the chain until an object processes security. Type: behavior mode. Overview: the responsibility chain mode and behavior mode indicate that they are mainly manifested by behavior. The behavior here can also be understood as Method ). The responsibility chain mode abstracts a pile of action objects for event processing into classes. Similar to the linked list structure, each class has a pointer pointing to its next class. In this way, when a class finishes processing the things it wants to handle, it will process the work for the next class. If the next class finds that it has nothing to do for the moment, it still transfers its responsibilities to the next class for processing. The responsibility chain model abstracts every responsibility into a class. Then there will be a series of roles to deal with something, so that a series of responsibilities can be abstracted. Then we chain them with pointers like linked lists to form the responsibility chain mode. A series of responsibilities abstract the same interface, and a pointer to form an abstract class as the abstract base class of this series of responsibility classes. For example, production line workers. Each person shall fulfill his/her responsibilities for the production of products, and then hand over his/her responsibilities to the next employee on the production line. Some production lines have requirements on the sequence of duties of employees and some production lines have no requirements. Class diagram: Participant: Client, initiating responsibility. Worker is an abstract base class that provides interfaces for all other workers to work. There is also a pointer pointing to the variables of the Worker class. Installer, Packer, QC, and three workers on the production line respectively implement their own work. In addition, they point to their next responsibilities. If they do not, they do not need to be pointed out. Sample Code: [csharp] using System; using System. collections. generic; using System. text; namespace Design14 {// abstract class Worker {public Worker worker; public abstract void DoHandler ();} class Installer: Worker {public Installer (Worker _ worker) {this. worker = _ worker;} public override void DoHandler () {Console. writeLine ("completing basic product assembly"); if (null! = This. worker) {this. worker. doHandler () ;}} class Packer: Worker {public Packer (Worker _ worker) {this. worker = _ worker;} public override void DoHandler () {Console. writeLine ("complete product packaging"); if (null! = This. worker) {this. worker. doHandler () ;}} class QC: Worker {public QC (Worker _ worker) {this. worker = _ worker;} public override void DoHandler () {Console. writeLine ("complete product quality control"); if (null! = This. worker) {this. worker. doHandler () ;}}// Client class Program {static void Main (string [] args) {// build Worker qc = new QC (null ); worker packer = new Packer (qc); Worker installer = new Installer (packer); // The installer is responsible for opening an account. doHandler ();}}}

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.