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

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

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

    人在江湖

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      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 人在江湖 閱讀(1710) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 亚洲综合无码一区二区三区| 国产AV无码专区亚洲AV男同 | 亚洲成年轻人电影网站www| 人妻免费久久久久久久了| 国产一级特黄高清免费大片| 99亚洲男女激情在线观看| 日韩在线免费看网站| 亚洲欧美不卡高清在线| 插B内射18免费视频| 亚洲色大网站WWW永久网站| 成人A级毛片免费观看AV网站| 国产精品高清视亚洲精品| 中文字幕无码不卡免费视频 | 久久水蜜桃亚洲AV无码精品| 永久免费bbbbbb视频| 国产亚洲视频在线观看| 亚洲成a人一区二区三区| 一边摸一边爽一边叫床免费视频| 亚洲无码高清在线观看| 免费日本一区二区| 亚洲综合久久1区2区3区| 最新免费jlzzjlzz在线播放| 亚洲高清乱码午夜电影网| 亚洲国产成人久久综合区| 久久不见久久见免费影院www日本| 自拍偷自拍亚洲精品情侣| 久久这里只精品99re免费| 亚洲国产美女福利直播秀一区二区| 久久WWW色情成人免费观看| 无码亚洲成a人在线观看| 狠狠亚洲狠狠欧洲2019| 精品一区二区三区免费毛片爱| 亚洲一级黄色大片| 国产一级一片免费播放| 99久久99这里只有免费的精品| 亚洲国产一区二区a毛片| 午夜视频在线观看免费完整版| 一个人免费观看日本www视频 | 亚洲 日韩 色 图网站| 久久亚洲av无码精品浪潮| 4399影视免费观看高清直播|