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
定义: 定义一个创建对象的接口,让子类决定实例哪一个类。工厂方法使一个类的实例化延迟到其子类。
定义一个创建对象的接口,让子类决定实例哪一个类。工厂方法使一个类的实例化延迟到其子类
类型:创建型。
创建型
适用场景:
优点:
1. 用户只需关心所需产品对应的工厂,无须关心创建细节; 2. 加入新产品符合开闭原则,提高可扩展性;
缺点:
1. 类的个数过容易过多,增加复杂度; 2. 增加系统的抽象性和理解难度;
UML 图:
示例代码:
public abstract class Product { }
public class ProductA extends Product { }
public class ProductB extends Product { }
public abstract class ProductFactory { public abstract Product getProduct(); }
public class ProductAFactory extends ProductFactory { public Product getProduct() { return new ProductA(); } }
public class ProductBFactory extends ProductFactory { public Product getProduct() { return new ProductB(); } }
public class Client { public static void main(String [] args){ ProductFactory productAFactory = new ProductAFactory(); Product productA = productAFactory.getProduct(); ProductFactory productBFactory = new ProductBFactory(); Product productB = productBFactory.getProduct(); } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
定义:
定义一个创建对象的接口,让子类决定实例哪一个类。工厂方法使一个类的实例化延迟到其子类
。类型:
创建型
。适用场景:
优点:
缺点:
UML 图:
示例代码:
The text was updated successfully, but these errors were encountered: