Java中如何使用Factory Method模式

发布时间:2021-11-30 17:08:07 作者:小新
来源:亿速云 阅读:148

这篇文章给大家分享的是有关Java中如何使用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: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 ...

现在,如果想改变程序中用到的 "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中如何使用Factory Method模式”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. angularjs如何封装$http为factory
  2. C++设计模式之Static Factory模式详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:数据库连接池dbcp的原理及配置是怎样的

下一篇:C/C++ Qt TreeWidget单层树形组件怎么应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》