【設計模式】【工廠方法】
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public interface Output
{
//接口裏定義的屬性隻能是常量
int MAX_CACHE_LINE = 50;
//接口裏定義的隻能是public的抽象實例方法
void out();
void getData(String msg);
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
//讓Printer類實現Output
public class Printer implements Output
{
private String[] printData = new String[MAX_CACHE_LINE];
//用以記錄當前需打印的作業數
private int dataNum = 0;
public void out()
{
//隻要還有作業,繼續打印
while(dataNum > 0)
{
System.out.println("打印機打印:" + printData[0]);
//把作業隊列整體前移一位,並將剩下的作業數減1
System.arraycopy(printData , 1, printData, 0, --dataNum);
}
}
public void getData(String msg)
{
if (dataNum >= MAX_CACHE_LINE)
{
System.out.println("輸出隊列已滿,添加失敗");
}
else
{
//把打印數據添加到隊列裏,已保存數據的數量加1。
printData[dataNum++] = msg;
}
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class BetterPrinter implements Output
{
private String[] printData = new String[MAX_CACHE_LINE * 2];
//用以記錄當前需打印的作業數
private int dataNum = 0;
public void out()
{
//隻要還有作業,繼續打印
while(dataNum > 0)
{
System.out.println("高速打印機正在打印:" + printData[0]);
//把作業隊列整體前移一位,並將剩下的作業數減1
System.arraycopy(printData , 1, printData, 0, --dataNum);
}
}
public void getData(String msg)
{
if (dataNum >= MAX_CACHE_LINE * 2)
{
System.out.println("輸出隊列已滿,添加失敗");
}
else
{
//把打印數據添加到隊列裏,已保存數據的數量加1。
printData[dataNum++] = msg;
}
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public interface OutputFactory
{
//僅定義一個方法用於返回輸出設備。
Output getOutput();
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class PrinterFactory
implements OutputFactory
{
public Output getOutput()
{
//該工廠隻負責產生Printer對象
return new Printer();
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class BetterPrinterFactory
implements OutputFactory
{
public Output getOutput()
{
//該工廠隻負責產生BetterPrinter對象
return new BetterPrinter();
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class Computer
{
private Output out;
public Computer(Output out)
{
this.out = out;
}
//定義一個模擬獲取字符串輸入的方法
public void keyIn(String msg)
{
out.getData(msg);
}
//定義一個模擬打印的方法
public void print()
{
out.out();
}
public static void main(String[] args)
{
//使用PrinterFactory子類來創建OutputFactory
OutputFactory of = new BetterPrinterFactory();
//將Output對象傳入,創建Computer對象
Computer c = new Computer(of.getOutput());
c.keyIn("輕量級Java EE企業應用實戰");
c.keyIn("瘋狂Java講義");
c.print();
}
}
最後更新:2017-04-04 07:03:36