We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
定义: 由一个工厂对象决定创建出哪一种产品类实例。
由一个工厂对象决定创建出哪一种产品类实例
类型:创建型,不属于 COF 23 种设计模式。
创建型,不属于 COF 23 种设计模式
适用场景:
优点:
只需要创建一个正确的参数,就可以获取所需的对象,而无需知道其创建细节。
缺点:
工厂类的职责相对过重,增加新的产品需要修改工厂类的判断逻辑,违背开闭原则。
UML 图:
示例代码:
public abstract class Product { }
public class ProductA extends Product { }
public class ProductB extends Product { }
public class ProductFactory { public Product getProduct(string className){ if("productA".equalsIgnoreCase(className)){ return new ProductA(); }else if ("prodctB".equalsIgnoreCase(className)){ return new ProductB(); } return null; } public Product getProduct(Class c){ try{ return (Product) Class.forName(c.getName()).newInstance(); } catch (Exception e){ e.printStackTrace(); } return null; } }
public class Client { public static void main(String [] args){ ProductFactory productFactory = new ProductFactory(); Product productA = productFactory.getProduct("productA"); Product productB = productFactory.getProduct(ProductB.class); } }
The text was updated successfully, but these errors were encountered:
SilenceHVK
No branches or pull requests
定义:
由一个工厂对象决定创建出哪一种产品类实例
。类型:
创建型,不属于 COF 23 种设计模式
。适用场景:
优点:
缺点:
UML 图:
示例代码:
The text was updated successfully, but these errors were encountered: