工廠方法模式的定義
定義一個(gè)用于創(chuàng)建對(duì)象的接口,讓子類決定實(shí)例化哪一個(gè)類踏揣。Factory Method使一個(gè)類的實(shí)例化延遲到子類输硝。
工廠方法模式的本質(zhì)
延遲到子類來(lái)選擇實(shí)現(xiàn)柬采。
工廠方法模式的優(yōu)缺點(diǎn)
優(yōu)點(diǎn): 更容易擴(kuò)展對(duì)象的新版本细移。
缺點(diǎn): 具體的產(chǎn)品對(duì)象和工廠方法是耦合的。
示例1
public interface ExportFileApi {
boolean export(String data);
}
public class ExportTxt implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到txt文件");
return true;
}
}
public class ExportDB implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到數(shù)據(jù)庫(kù)");
return true;
}
}
public abstract class ExportOperate {
public boolean export(String data) {
ExportFileApi api = factoryMethod();
return api.export(data);
}
/**
* 鉤子方法熊锭,由子類去選擇創(chuàng)建具體的類
* @return
*/
protected abstract ExportFileApi factoryMethod();
}
public class ExportOperateTxt extends ExportOperate {
@Override
protected ExportFileApi factoryMethod() {
return new ExportTxt();
}
}
public class ExportOperateDB extends ExportOperate {
@Override
protected ExportFileApi factoryMethod() {
return new ExportDB();
}
}
public class TestClient {
public static void main(String[] args) {
ExportOperate operate = new ExportOperateDB();
operate.export("aaaa");
}
}
去擴(kuò)展一個(gè)導(dǎo)出xml弧轧。
public class ExportXml implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到xml");
return true;
}
}
public class ExportOperateXml extends ExportOperate {
@Override
protected ExportFileApi factoryMethod() {
return new ExportXml();
}
}
public class TestClient {
public static void main(String[] args) {
ExportOperate operate = new ExportOperateXml();
operate.export("aaaa");
}
}
示例2 參數(shù)化
public interface ExportFileApi {
boolean export(String data);
}
public class ExportTxt implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到txt文件");
return true;
}
}
public class ExportDB implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到數(shù)據(jù)庫(kù)");
return true;
}
}
public class ExportXml implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到xml");
return true;
}
}
public class ExportOperate {
public boolean export(int type,String data) {
ExportFileApi api = factoryMethod(type);
return api.export(data);
}
protected ExportFileApi factoryMethod(int type) {
ExportFileApi api = null;
if (1 == type) {
api = new ExportTxt();
} else if (2 == type){
api = new ExportDB();
} else if (3 == type) {
api = new ExportXml();
}
return api;
}
}
public class TestClient {
public static void main(String[] args) {
ExportOperate op = new ExportOperate();
op.export(2, "aaa");
}
}
擴(kuò)展導(dǎo)出到MySQL
public class ExportDBMySQL implements ExportFileApi {
@Override
public boolean export(String data) {
System.out.println("導(dǎo)出數(shù)據(jù):"+data+"到數(shù)據(jù)庫(kù)MySQL");
return true;
}
}
public class ExportOperate2 extends ExportOperate {
@Override
protected ExportFileApi factoryMethod(int type) {
if (2 == type) {
return new ExportDBMySQL();
} else {
return super.factoryMethod(type);
}
}
}
public class TestClient {
public static void main(String[] args) {
ExportOperate op = new ExportOperate2();
op.export(2, "aaa");
}
}