<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
    主站蜘蛛池模板: 韩国亚洲伊人久久综合影院| 最近中文字幕免费mv视频8| 青青青亚洲精品国产| 国产乱子精品免费视观看片| 日本免费网站在线观看| 国产在线ts人妖免费视频| 国产亚洲成av片在线观看| 久久亚洲AV成人无码国产| 亚洲综合小说另类图片动图| 国产成人亚洲精品91专区高清| 久9热免费精品视频在线观看| 丁香花在线观看免费观看| 337p日本欧洲亚洲大胆裸体艺术| 亚洲色图综合网站| 国产精品综合专区中文字幕免费播放| 亚洲欧美成人av在线观看| 免费无码又爽又刺激网站直播| 成年人免费观看视频网站| 精品无码专区亚洲| 亚洲国产精品无码久久久久久曰| 91在线亚洲精品专区| 999国内精品永久免费观看| 亚洲人AV在线无码影院观看| 国产特级淫片免费看| 国产A∨免费精品视频| 亚洲国产一区在线| 一级特黄aaa大片免费看| 毛片高清视频在线看免费观看| 亚洲av永久无码天堂网| 无码一区二区三区免费| 亚洲av永久无码精品三区在线4| 一级一看免费完整版毛片| 久久精品国产亚洲AV麻豆~| 黄 色一级 成 人网站免费| 亚洲综合成人网在线观看| 天天看片天天爽_免费播放| 亚洲不卡视频在线观看| 婷婷亚洲天堂影院| 美女黄频视频大全免费的| 最近免费中文字幕4| 精品国产免费人成网站|