使用Factory Method模式 (引用lhbdir )

          Posted on 2007-11-15 23:05 yukui 閱讀(104) 評論(0)  編輯  收藏 所屬分類: 技術
          A: Factory method(工廠方法)只不過是實例化對象的一種方法的名稱。就象工廠一樣,F(xiàn)actory method的任務是創(chuàng)建--或制造--對象。

          讓我們看一個例子。

          每個程序要有一種報錯的方式。看看下面的接口:代碼清單1
          public interface Trace {

                // turn on and off debugging
                public void setDebug( boolean debug );

                // write out a debug message
                public void debug( String message );

                // write out an error message
                public void error( String message );

          }

          假設寫了兩個實現(xiàn)。一個實現(xiàn)(代碼清單3)將信息寫到命令行,另一個(代碼清單2)則寫到文件中。

          代碼清單2
          public class FileTrace implements Trace {
                   
                private java.io.PrintWriter pw;
                private boolean debug;

                public FileTrace() throws java.io.IOException {
                      // a real FileTrace would need to obtain the filename somewhere
                      // for the example I'll hardcode it
                      pw = new java.io.PrintWriter( new java.io.FileWriter( "c:\trace.log" ) );
                }

                public void setDebug( boolean debug ) {
                      this.debug = debug;
                }

                public void debug( String message ) {
                      if( debug ) {  // only print if debug is true
                            pw.println( "DEBUG: " + message );
                            pw.flush();
                      }
                }
                public void error( String message ) {
                      // always print out errors
                      pw.println( "ERROR: " + message );
                      pw.flush();
                }

          }

          代碼清單3
          public class SystemTrace implements Trace {

                private boolean debug;

                public void setDebug( boolean debug ) {
                      this.debug = debug;
                }

                public void debug( String message ) {
                      if( debug ) {  // only print if debug is true
                            System.out.println( "DEBUG: " + message );
                      }
                }
                public void error( String message ) {
                      // always print out errors
                      System.out.println( "ERROR: " + message );
                }

          }

          要使用這兩個類中的任一個,需要這樣做:

          代碼清單4
          //... some code ...
          SystemTrace log = new SystemTrace();
          //... code ...
          log.debug( "entering loog" );
          // ... etc ...

          現(xiàn)在,如果想改變程序中用到的 "Trace實現(xiàn)",就需要修改實例化 "Trace實現(xiàn)" 的每個類。使用了Trace的類的數(shù)量可能很多,這種修改就需要大量的工作。而且,你一定也想盡可能地避免大量修改你的類。

          代碼清單5
          public class TraceFactory {
                public static Trace getTrace() {
                      return new SystemTrace();
                }
          }

          getTrace()是一個Factory method。這樣,無論什么時候你想得到一個Trace的引用,只用簡單地調(diào)用TraceFactory.getTrace():

          代碼清單6
          //... some code ...
          Trace log = new TraceFactory.getTrace(); 

          posts - 131, comments - 12, trackbacks - 0, articles - 32

          Copyright © yukui

          主站蜘蛛池模板: 陵水| 海安县| 尼勒克县| 德格县| 偃师市| 星座| 宜良县| 内丘县| 额尔古纳市| 塘沽区| 阿城市| 平邑县| 凤凰县| 邓州市| 青冈县| 高安市| 来安县| 赤城县| 湾仔区| 阳春市| 乐清市| 镇原县| 新郑市| 托克逊县| 宁乡县| 通河县| 遂宁市| 金平| 莱州市| 高陵县| 宜兴市| 新蔡县| 汾阳市| 竹山县| 米脂县| 毕节市| 盐津县| 神木县| 永胜县| 沾益县| 夹江县|