溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java中如何使用Factory Method模式

發(fā)布時(shí)間:2021-11-30 17:08:07 來(lái)源:億速云 閱讀:163 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)Java中如何使用Factory Method模式的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

Q: 閱讀 "Polymorphism in its purest form" 一文時(shí),我看到了一個(gè)不熟悉的術(shù)語(yǔ) "Factory method"。你能解釋一下什么是Factory method并說(shuō)明如何使用它嗎?

A: Factory method(工廠方法)只不過(guò)是實(shí)例化對(duì)象的一種方法的名稱。就象工廠一樣,F(xiàn)actory method的任務(wù)是創(chuàng)建--或制造--對(duì)象。

讓我們看一個(gè)例子。

每個(gè)程序要有一種報(bào)錯(cuò)的方式??纯聪旅娴慕涌冢?/p>

代碼清單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 );

}

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

代碼清單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 );
  }

}

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

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

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

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

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

代碼清單6
//... some code ...
Trace log = new TraceFactory.getTrace();
//... code ...
log.debug( "entering loog" );
// ... etc ...

使用Factory method來(lái)獲得實(shí)例可以大量節(jié)省以后的工作。上面的代碼中,TraceFactory返回的是SystemTrace實(shí)例。假設(shè)需求發(fā)生了變化,需要將信息寫(xiě)到文件中。如果是使用Factory method來(lái)獲得實(shí)例,只用在一個(gè)類中修改一次就可以滿足新的需求。你就不用在使用了Trace的的每個(gè)類中進(jìn)行修改了。也就是說(shuō),只用簡(jiǎn)單地重定義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;
  }
  }
}

當(dāng)不能確定一個(gè)類的什么具體實(shí)現(xiàn)要被實(shí)例化時(shí),F(xiàn)actory method會(huì)很有用。你可以將那些細(xì)節(jié)留給Factory method。

在上面的例子中,你的程序不知道要?jiǎng)?chuàng)建FileTrace還是SystemTrace。因而,你可以只是用Trace來(lái)處理對(duì)象,對(duì)具體實(shí)現(xiàn)的實(shí)例化則留給Factory method。

感謝各位的閱讀!關(guān)于“Java中如何使用Factory Method模式”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI