服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java q&a: 使用factory method模式

java q&a: 使用factory method模式

q: 阅读 "polymorphism in its purest form" 一文时,我看到了一个不熟悉的术语 "factory method"。你能解释一下什么是factory method并说明如何使用它吗?

a: factory method(工厂方法)只不过是实例化对象的一种方法的名称。就象工厂一样,factory method的任务是创建--或制造--对象。

让我们看一个例子。

每个程序要有一种报错的方式。看看下面的接口:

代码清单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 );

}

假设写了两个实现。一个实现(代码清单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: race.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 ...

现在,如果想改变程序中用到的 "trace实现",就需要修改实例化 "trace实现" 的每个类。使用了trace的类的数量可能很多,这种修改就需要大量的工作。而且,你一定也想尽可能地避免大量修改你的类。

代码清单5
public class tracefactory {
      public static trace gettrace() {
            return new systemtrace();
      }
}

gettrace()是一个factory method。这样,无论什么时候你想得到一个trace的引用,只用简单地调用tracefactory.gettrace():

代码清单6
//... some code ...
trace log = new tracefactory.gettrace();
//... code ...
log.debug( "entering loog" );
// ... etc ...

使用factory method来获得实例可以大量节省以后的工作。上面的代码中,tracefactory返回的是systemtrace实例。假设需求发生了变化,需要将信息写到文件中。如果是使用factory method来获得实例,只用在一个类中修改一次就可以满足新的需求。你就不用在使用了trace的的每个类中进行修改了。也就是说,只用简单地重定义gettrace():

代码清单7
public class tracefactory {
      public static trace gettrace() {
            try {
                  return new filetrace();
            } catch ( java.io.ioexception ex ) {
                  trace t = new systemtrace();
                  t.error( "could not instantiate filetrace: " + ex.getmessage() );
                  return t;
            }
      }
}

当不能确定一个类的什么具体实现要被实例化时,factory method会很有用。你可以将那些细节留给factory method。

在上面的例子中,你的程序不知道要创建filetrace还是systemtrace。因而,你可以只是用trace来处理对象,对具体实现的实例化则留给factory method。

扫描关注微信公众号