1700457440
public<T extends Product>T createProduct(Class<T>c){
1700457441
1700457442
Product product=null;
1700457443
1700457444
try{
1700457445
1700457446
product=(Product)Class.forName(c.getName()).newInstance();
1700457447
1700457448
}catch(Exception e){
1700457449
1700457450
//异常处理
1700457451
1700457452
}
1700457453
1700457454
return(T)product;
1700457455
1700457456
}
1700457457
1700457458
}
1700457459
1700457460
场景类的调用方法如代码清单8-12所示。
1700457461
1700457462
代码清单8-12 场景类
1700457463
1700457464
public class Client{
1700457465
1700457466
public static void main(String[]args){
1700457467
1700457468
Creator creator=new ConcreteCreator();
1700457469
1700457470
Product product=creator.createProduct(ConcreteProduct1.class);
1700457471
1700457472
/*
1700457473
1700457474
*继续业务处理
1700457475
1700457476
*/
1700457477
1700457478
}
1700457479
1700457480
}
1700457481
1700457482
该通用代码是一个比较实用、易扩展的框架,读者可以根据实际项目需要进行扩展。
1700457483
1700457484
1700457485
1700457486
1700457488
设计模式之禅 8.3 工厂方法模式的应用
1700457489
[
上一页 ]
[ :1.70045744e+09 ]
[
下一页 ]