<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 人在江湖 閱讀(1714) 評論(0)  編輯  收藏 所屬分類: design pattern
    主站蜘蛛池模板: 久久久久亚洲AV无码专区首| 四虎永久成人免费| 亚洲精品在线播放| a级毛片视频免费观看| 在线看无码的免费网站| 亚洲av日韩av无码黑人| 免费观看91视频| 日韩毛片免费在线观看| 亚洲国产精华液2020| 噜噜嘿在线视频免费观看| 亚洲精品国产电影| 亚洲精品偷拍视频免费观看| 亚洲狠狠爱综合影院婷婷| 一级毛片在线免费视频| 丝袜熟女国偷自产中文字幕亚洲| 亚洲av日韩av无码av| 成年男女男精品免费视频网站| 亚洲精品无播放器在线播放| 国产a不卡片精品免费观看| 黄色网址大全免费| 日韩精品无码专区免费播放| 亚洲网站在线免费观看| 最近最新的免费中文字幕| 日韩欧美亚洲中文乱码| 国产日产亚洲系列最新| 国产精品免费高清在线观看| 亚洲国产精品13p| 青青操免费在线视频| 亚洲国产精品无码久久久| 精品国产免费一区二区| caoporn成人免费公开| 午夜亚洲国产理论秋霞| 成人免费视频软件网站| fc2免费人成在线视频| 亚洲一区二区三区四区在线观看| 午夜成人无码福利免费视频| 久久亚洲国产精品五月天| 岛国片在线免费观看| 国产性生大片免费观看性| 亚洲国产综合在线| 亚洲欧洲中文日韩久久AV乱码|