国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

使用Factory Method模式

2019-11-18 14:29:03
字體:
供稿:網(wǎng)友

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

A: Factory method(工廠方法)只不過是實例化對象的一種方法的名稱。就象工廠一樣,F(xiàn)actory method的任務(wù)是創(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 );

}

假設(shè)寫了兩個實現(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: 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 ...

現(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();
//... code ...
log.debug( "entering loog" );
// ... etc ...

使用Factory method來獲得實例可以大量節(jié)省以后的工作。上面的代碼中,TraceFactory返回的是SystemTrace實例。假設(shè)需求發(fā)生了變化,需要將信息寫到文件中。假如是使用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;
}
}
}

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 乌苏市| 枞阳县| 吉林市| 饶平县| 曲靖市| 临湘市| 新乡县| 惠来县| 乌苏市| 保山市| 铜梁县| 易门县| 黄梅县| 石首市| 镇江市| 威宁| 德昌县| 兴宁市| 荥阳市| 达日县| 新安县| 贵阳市| 隆德县| 神池县| 永定县| 尖扎县| 玛多县| 五家渠市| 西青区| 探索| 噶尔县| 紫金县| 财经| 民勤县| 东明县| 凤山县| 彰化县| 左云县| 湖北省| 兴山县| 山东|