Java的原廠模式例子

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.