<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 人在江湖 閱讀(1703) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 久久亚洲2019中文字幕| 成人在线视频免费| 久久精品国产亚洲Aⅴ蜜臀色欲| 婷婷亚洲久悠悠色悠在线播放| 一级黄色毛片免费看| 亚洲第一永久AV网站久久精品男人的天堂AV | 最近中文字幕mv免费高清电影| 亚洲黄色在线视频| 久久WWW免费人成一看片| 久久久久亚洲AV无码专区体验| 毛片在线全部免费观看| 国产v亚洲v天堂无码网站| baoyu777永久免费视频| 最近中文字幕无免费| 四虎成人精品在永久免费| 久久亚洲中文字幕无码| 免费吃奶摸下激烈视频| 免费人成视频在线观看免费| 免费国产小视频在线观看| 日本永久免费a∨在线视频| 国产亚洲?V无码?V男人的天堂| 一级黄色免费网站| 亚洲国产人成网站在线电影动漫 | 亚洲图片一区二区| 成人免费一级毛片在线播放视频 | 国产成人A在线观看视频免费| 亚洲成a∨人片在无码2023 | 国产色爽免费视频| 久久精品无码免费不卡| 亚洲av永久无码精品表情包| 久久久久久精品成人免费图片| 亚洲综合激情五月色一区| 亚洲精品人成无码中文毛片| APP在线免费观看视频| 亚洲成a人片在线不卡| 无码国产精品一区二区免费3p | 国产伦精品一区二区三区免费下载| 国产A∨免费精品视频| 亚洲毛片基地日韩毛片基地| 无码日韩精品一区二区免费暖暖 | 五月婷婷在线免费观看|