Based on. NET's responsibility chain pattern class library--nchain

Source: Internet
Author: User
Tags bool

Chain.net (also known as Nchain) is an implementation of the responsibility chain model on the. NET and Mono platforms. Its version 0.1 (already accessible in SourceForge) combines the standard chain of responsibilities and command patterns to "provide a convenient and flexible solution for command-based functionality."

Nchain loosely based on the Jakarta Commons chain package on the Java platform. In general, the responsibility chain model decomposes the command object from a series of processing units to decouple it. Each processing unit contains the appropriate code to describe the type of command object it can accept, and it also delegates part of the responsibility to handle objects that do not match the next processing unit on the responsibility chain.

The following is an example of a simple responsibility chain pattern:

using System;


using System.Collections;


namespace Chain_of_responsibility


{


public Interface Ichain


    {


bool Process (Object command);


    }


public class Chain


    {


private ArrayList _list;


Public ArrayList List


        {


Get


            {


return _list;


            }


        }


public Chain ()


        {


_list = new ArrayList ();


        }


public void Message (Object command)


        {


foreach (Ichain item in _list)


            {


bool result = Item. Process (command);


if (result = = true) break;


            }


        }


public void Add (Ichain handler)


        {


List.add (handler);


        }


    }


public class Stringhandler:ichain


    {


public bool Process (Object command)


        {


if (command is string)


            {


Console.WriteLine ("Stringhandler can handle this message


: {0} ", (string) command);


return true;


            }


return false;


        }


    }


public class Integerhandler:ichain


    {


public bool Process (Object command)


        {


if (command is int)


            {


Console.WriteLine ("Integerhandler can handle this message


: {0} ", (int) command);


return true;


            }


return false;


        }


    }





class Testmain


    {


static void Main (string[] args)


        {


Chain Chain = new Chain ();


chain. ADD (New Stringhandler ());


Chain. ADD (New Integerhandler ());


chain. Message ("1st string value");


chain. Message (100);


        }


    }


}

Nchain provides a more similar, but stronger, architecture.

Nchain requires further testing and performance monitoring to determine whether it applies to the enterprise application architecture. This is an open source project and provides a useful introductory example to get started quickly. At the moment, Nchain will have a certain use in scenarios in which the command mode is used, and different types of commands need to be executed according to context.

. NET Chain of Responsibility Library

Http://www.infoq.com/news/2008/09/nchain

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.