設(shè)計(jì)模式源碼 https://github.com/iluwatar/java-design-patterns
基本概念
FactoryMethod是一種創(chuàng)建性模式,它定義了一個(gè)創(chuàng)建對(duì)象的接口,但是卻讓子類(lèi)來(lái)決定具體實(shí)例化哪一個(gè)類(lèi).當(dāng)一個(gè)類(lèi)無(wú)法預(yù)料要?jiǎng)?chuàng)建哪種類(lèi)的對(duì)象或是一個(gè)類(lèi)需要由子類(lèi)來(lái)指定創(chuàng)建的對(duì)象時(shí)我們就需要用到Factory Method 模式了.簡(jiǎn)單說(shuō)來(lái),Factory Method可以根據(jù)不同的條件產(chǎn)生不同的實(shí)例,當(dāng)然這些不同的實(shí)例通常是屬于相同的類(lèi)型,具有共同的父類(lèi).Factory Method把創(chuàng)建這些實(shí)例的具體過(guò)程封裝起來(lái)了,簡(jiǎn)化了客戶(hù)端的應(yīng)用,也改善了程序的擴(kuò)展性,使得將來(lái)可以做最小的改動(dòng)就可以加入新的待創(chuàng)建的類(lèi). 通常我們將Factory Method作為一種標(biāo)準(zhǔn)的創(chuàng)建對(duì)象的方法,當(dāng)發(fā)現(xiàn)需要更多的靈活性的時(shí)候,就開(kāi)始考慮向其它創(chuàng)建型模式轉(zhuǎn)化
/**
*
* The Factory Method is a creational design pattern which uses factory methods to deal with the
* problem of creating objects without specifying the exact class of object that will be created.
* This is done by creating objects via 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.
* <p>
* In this Factory Method example we have an interface ({@link Blacksmith}) with a method for
* creating objects ({@link Blacksmith#manufactureWeapon}). The concrete subclasses (
* {@link OrcBlacksmith}, {@link ElfBlacksmith}) then override the method to produce objects of
* their liking.
*
*/
模式結(jié)構(gòu)

diagram1.png
源碼分析
創(chuàng)建對(duì)象接口類(lèi)
/**
*
* The interface containing method for producing objects.
*
*/
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
創(chuàng)建對(duì)象的實(shí)現(xiàn)類(lèi)
/**
*
* Concrete subclass for creating new objects.
*
*/
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new ElfWeapon(weaponType);
}
}
測(cè)試
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
private final Blacksmith blacksmith;
/**
* Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture
* the weapons for war.
* <code>App</code> is unaware which concrete implementation of {@link Blacksmith} it is using.
* The decision of which blacksmith implementation to use may depend on configuration, or
* the type of rival in war.
* @param blacksmith a non-null implementation of blacksmith
*/
public App(Blacksmith blacksmith) {
this.blacksmith = blacksmith;
}
/**
* Program entry point
*
* @param args command line args
*/
public static void main(String[] args) {
// Lets go to war with Orc weapons
App app = new App(new OrcBlacksmith());
app.manufactureWeapons();
// Lets go to war with Elf weapons
app = new App(new ElfBlacksmith());
app.manufactureWeapons();
}
private void manufactureWeapons() {
Weapon weapon;
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
LOGGER.info(weapon.toString());
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
LOGGER.info(weapon.toString());
}