【設(shè)計(jì)模式】- factory-method

設(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());
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容