1. 簡單工廠:
(1). 建立輸出(Output)介面
package stone;
public interface Output {
public int MAX_COUNT =10; // 最多可列印多少條記錄
public void show(); // 列印
public void addData(String msg); // 把要列印的記錄加入到列印排序
}
(2). 建立印表機類, 其繼承輸出介面
package stone;
public class Printer implements Output {
private String[] data_array= new String[MAX_COUNT]; // 儲存要列印資訊的數組
private int dataNum=0; // 要列印資訊的個數
public void addData(String msg) {
if(dataNum>MAX_COUNT) // 要列印的資訊不能大士最大值
System.out.println("Print queue is max");
else
data_array[dataNum++]=msg;
}
public void show() {
while(dataNum>0){
System.out.println("Print:"+ data_array[0]);
System.arraycopy(data_array, 1, data_array, 0, dataNum--);
}
}
}
(3). 建立輸出在工廠, 此工廠定義用哪一個印表機來列印.
package stone;
public class OutputFactory {
public static Output output(){
return new Printer();
}
}
(4). 測試原廠模式的列印情況:
package stone;
public class TestSimpleFactory {
private Output output;
public TestSimpleFactory(Output output) {
this.output=output;
}
private void dataIn(String msg){
output.addData(msg);
}
private void dataOut(){
output.show();
}
public static void main(String[] args) {
TestSimpleFactory sf= new TestSimpleFactory(OutputFactory.output());
sf.dataIn("Stone Test Simple Factory 1");
sf.dataIn("Stone Test Simple Factory 2");
sf.dataOut();
}
}
輸出結果如下:
Print:Stone Test Simple Factory 1
Print:Stone Test Simple Factory 2
(5) 添加另一個印表機類:
package stone;
public class Printer2 implements Output {
private String[] data_array= new String[MAX_COUNT];
private int dataNum=0;
public void addData(String msg) {
if(dataNum>MAX_COUNT)
System.out.println("Print queue is max");
else
data_array[dataNum++]=msg;
}
public void show() {
while(dataNum>0){
System.out.println("Other Print:"+ data_array[0]);
System.arraycopy(data_array, 1, data_array, 0, dataNum--);
}
}
}
(6). 當要使用另一個印表機時,僅需要改下工廠類就行了
package stone;
public class OutputFactory {
public static Output output(){
// return new Printer();
return new Printer2();
}
}
(7) 運行 TestSimpleFactory , 結果如下:
Other Print:Stone Test Simple Factory 1
Other Print:Stone Test Simple Factory 2
------------------------------------------------------------------------------------------------
2. Factory 方法
(8). 為每一個印表機添加一個在工廠:
package stone;
public class PrinterFactory {
public static Output output(){
return new Printer();
}
}
package stone;
public class Printer2Factory {
public static Output output(){
return new Printer2();
}
}
(9). 為印表機工廠 添加 工廠:
package stone;
public class OutputFactoryFactory {
public static Output outputFactory(String msg){
if(msg.equalsIgnoreCase("printer2"))
return Printer2Factory.output();
else
return PrinterFactory.output();
}
}
(10). 運行 TestFactoryMethod 來測試 Factory 方法:
package stone;
public class TestFactoryMethod {
private Output output;
public TestFactoryMethod(Output output) {
this.output=output;
}
private void dataIn(String msg){
output.addData(msg);
}
private void dataOut(){
output.show();
}
public static void main(String[] args) {
TestFactoryMethod sf= new TestFactoryMethod(OutputFactoryFactory.outputFactory("printer2"));
sf.dataIn("Stone Test Simple Factory 1");
sf.dataIn("Stone Test Simple Factory 2");
sf.dataOut();
}
}
測試結果如下:
Other Print:Stone Test Simple Factory 1
Other Print:Stone Test Simple Factory 2