定义:工厂方法模式属于建立型设计模式。定义一个用于建立对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。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设计模式