工厂模式获取指定对象:
public final static Map<String, Supplier<Product>> map = new HashMap<>();
static {
map.put("loan", Loan::new);
map.put("stock", Stock::new);
}
public static Product createProduct(String name) {
Supplier<Product> productSupplier = map.get(name);
if (productSupplier != null) {
return productSupplier.get();
}
throw new IllegalArgumentException("No such product " + name);
}
@Test
public void testP(){
Product loan = createProduct("loan");
System.out.println(loan.toString());
}