定義:工廠方法模式屬於建立型設計模式。定義一個用於建立對象的接口,讓子類決定實例化哪一個類。工廠方法使一個類的實例化延遲到其子類。git
public abstract class AbstractLivingDetection {
/**
* 開始檢測
*/
public abstract void startDetection();
}
複製代碼
public class HaiXinLivingDetection extends AbstractLivingDetection {
@Override
public void startDetection() {
System.out.println("開啓海鑫活體檢測");
}
}
複製代碼
public class TongFuDunLivingDetection extends AbstractLivingDetection {
@Override
public void startDetection() {
System.out.println("開啓通付盾活體檢測");
}
}
複製代碼
public abstract class AbstractLivingDetectionFactory {
public abstract <T extends AbstractLivingDetection> T createLivingDetection(Class<T> t);
}
複製代碼
public class LivingDetectionFactory extends AbstractLivingDetectionFactory {
@Override
public <T extends AbstractLivingDetection>T createLivingDetection(Class<T> t){
try {
return t.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
複製代碼
#####使用場景github
代碼已上傳github設計模式