网页功能: 加入收藏 设为首页 网站搜索  
Java Q&A: 使用Factory Method模式
发表日期:2004-07-30作者:[转贴] 出处:  

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。


 

我来说两句】 【加入收藏】 【返加顶部】 【打印本页】 【关闭窗口
中搜索 Java Q&A: 使用Factory Method模式
本类热点文章
  Java读取文件中含有中文的解决办法
  Java读取文件中含有中文的解决办法
  简单加密/解密方法包装, 含encode(),de..
  EJB 3.0规范全新体验
  java简单的获取windows系统网卡mac地址
  让Java程序带着JRE一起上路
  抢先体验"野马"J2SE6.0
  Java连接各种数据库的实例
  Java连接各种数据库的实例
  JAVA的XML编程实例解析
  Java学习从入门到精通(附FAQ)
  新手必读:Java学习的捷径
最新分类信息我要发布 
最新招聘信息

关于我们 / 合作推广 / 给我留言 / 版权举报 / 意见建议 / 广告投放  
Copyright ©2003-2024 Lihuasoft.net webmaster(at)lihuasoft.net
网站编程QQ群   京ICP备05001064号 页面生成时间:0.01095