<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    人在江湖

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      82 Posts :: 10 Stories :: 169 Comments :: 0 Trackbacks

    The following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. The output is:

    Writing to stdout: Entering function y. Writing to stdout: Step1 completed. Sending via e-mail: Step1 completed. Writing to stdout: An error has occurred. Sending via e-mail: An error has occurred. Writing to stderr: An error has occurred.

    Note that this example should not be seen as a recommendation on how to write logging classes.

    Also, note that in a 'pure' implementation of the chain of responsibility pattern, a logger would not pass responsibility further down the chain after handling a message. In this example, a message will be passed down the chain whether it is handled or not.

    import java.util.*;
    abstract class Logger
    {
        public static int ERR = 3;
        public static int NOTICE = 5;
        public static int DEBUG = 7;
        protected int mask;
        // The next element in the chain of responsibility
        protected Logger next;
        public Logger setNext( Logger l )
        {
            next = l;
            return l;
        }
        public void message( String msg, int priority )
        {
            if ( priority <= mask )
            {
                writeMessage( msg );
            }
            if ( next != null )
            {
                next.message( msg, priority );
            }
        }
        abstract protected void writeMessage( String msg );
    }
    class StdoutLogger extends Logger
    {
        public StdoutLogger( int mask ) { this.mask = mask; }
        protected void writeMessage( String msg )
        {
            System.out.println( "Writing to stdout: " + msg );
        }
    }
    class EmailLogger extends Logger
    {
        public EmailLogger( int mask ) { this.mask = mask; }
        protected void writeMessage( String msg )
        {
            System.out.println( "Sending via email: " + msg );
        }
    }
    class StderrLogger extends Logger
    {
        public StderrLogger( int mask ) { this.mask = mask; }
        protected void writeMessage( String msg )
        {
            System.err.println( "Sending to stderr: " + msg );
        }
    }
    public class ChainOfResponsibilityExample
    {
        public static void main( String[] args )
        {
            // Build the chain of responsibility
            Logger l,l1;
            l1 = l = new StdoutLogger( Logger.DEBUG );
            l1 = l1.setNext(new EmailLogger( Logger.NOTICE ));
            l1 = l1.setNext(new StderrLogger( Logger.ERR ));
            // Handled by StdoutLogger
            l.message( "Entering function y.", Logger.DEBUG );
            // Handled by StdoutLogger and EmailLogger
            l.message( "Step1 completed.", Logger.NOTICE );
            // Handled by all three loggers
            l.message( "An error has occurred.", Logger.ERR );
        }
    }

    posted on 2011-02-12 23:17 人在江湖 閱讀(1703) 評(píng)論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 亚洲av无码一区二区三区网站| 一级特级aaaa毛片免费观看| 国产亚洲免费的视频看| 暖暖日本免费在线视频| **真实毛片免费观看| 国产成人AV免费观看| 免费无遮挡无码视频在线观看| 亚洲日本久久久午夜精品| 18gay台湾男同亚洲男同| 亚洲精品网站在线观看不卡无广告 | 毛片亚洲AV无码精品国产午夜| 亚洲国产成人久久99精品| 亚洲国产美国国产综合一区二区| 亚洲男人第一无码aⅴ网站| 免费无码看av的网站| 一个人免费高清在线观看| 50岁老女人的毛片免费观看 | 亚洲A∨无码无在线观看| 亚洲天堂中文字幕在线| 全亚洲最新黄色特级网站 | 国产免费人人看大香伊| 德国女人一级毛片免费| 青娱乐免费在线视频| 五月婷婷综合免费| 国产高清免费视频| 久久www免费人成看片| xxxx日本免费| 2021免费日韩视频网| 久草免费在线观看视频| 最近免费2019中文字幕大全| 久久久精品免费视频| 日韩视频免费在线观看| 日本一区二区免费看| 日本免费人成网ww555在线 | 亚洲欧洲日产国产综合网| 久久久久久久久亚洲| 7777久久亚洲中文字幕蜜桃| 亚洲自偷自拍另类图片二区| 亚洲午夜国产精品无卡| 99久久国产亚洲综合精品| 亚洲日韩av无码中文|