英文原文链接
单例模式样例代码
Virtual Constructor 虚拟构造器
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
定义一个用于创建对象的接口,但让子类决定实例化哪个类。工厂方法允许类的实例化方法到子类中进行。
Real world example
现实生活中的例子
Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. Depending on the customer at hand the right type of blacksmith is summoned.
铁匠制造武器。精灵需要精灵武器,兽人需要兽人武器。根据(手头)顾客(的不同类型),铁匠给予不同的(武器)类型。
It provides a way to delegate the instantiation logic to child classes.
它提供了一种将实例化逻辑委托给子类的方式。
#Wikipedia says 维基百科中的描述
In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor.
在基于类的编程中,工厂方法模式是一种创造性模式,它使用工厂方法来处理创建对象的问题,而无需指定将要创建的对象的确切类。通过调用工厂方法(在接口中指定并由子类实现,或在基类中实现并可由派生类覆盖)来创建对象,而不是通过调用构造函数(来创建对象)。
上面这段长句不好理解,我给断开分成短句,英语好的自行忽略:
在基于类的编程中
#Programmatic Example 程序实例
以我们的铁匠例子为例。首先,我们有一个铁匠接口和一些实现方法。
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new ElfWeapon(weaponType);
}
}
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new OrcWeapon(weaponType);
}
}
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
现在,随着客户要求的不同,将召唤不同类型的铁匠来制造指定类型的武器。
Blacksmith blacksmith = new ElfBlacksmith();
blacksmith.manufactureWeapon(WeaponType.SPEAR);
blacksmith.manufactureWeapon(WeaponType.AXE);
// Elvish weapons are created
#Applicability 适用场景
Use the Factory Method pattern when
当遇到下列场景时使用工厂方法模式
#Presentations 演示文稿
Factory Method Pattern
该文稿内容和本文大体一致,仅多了Diagram部分内容,详情如下
##Diagram 图解
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-S9IYA37O-1588757398271)(http://java-design-patterns.com/patterns/factory-method/etc/diagram1.png)]
#Real world examples 现有样例
#Credits 参考文献
Design Patterns: Elements of Reusable Object-Oriented Software
通过意图(Intent)发现,工厂方法的满足条件为“父类提供规范,让子类进行实例化”。
封装创建过程,构造特定类型的对象实例。
与建造者模式(Builder)区别:Builder创建复杂对象,对象之间没有直接联系,对象类型比较『杂乱』。
而工厂方法模式用于创建特定类型对象,当需要扩展对象创建功能时,新实现并实现对象接口,即可创建新的类型对象
深入浅出设计模式——工厂方法模式(Factory Method)