Skip to content
百科百科
操作系统
设计模式
算法
题解
java
leetcode
  • 设计模式
    • /设计模式/设计模式 - 单例.md
      • /设计模式/设计模式 - 中介者.md
        • /设计模式/设计模式 - 享元.md
          • /设计模式/设计模式 - 代理.md
            • /设计模式/设计模式 - 原型模式.md
              • /设计模式/设计模式 - 命令.md
                • /设计模式/设计模式 - 备忘录.md
                  • /设计模式/设计模式 - 外观.md
                    • /设计模式/设计模式 - 工厂方法.md
                      • 工厂方法(Factory Method)
                        • Intent
                          • Class Diagram
                            • Implementation
                              • JDK
                            • /设计模式/设计模式 - 抽象工厂.md
                              • /设计模式/设计模式 - 桥接.md
                                • /设计模式/设计模式 - 模板方法.md
                                  • /设计模式/设计模式 - 状态.md
                                    • /设计模式/设计模式 - 生成器.md
                                      • /设计模式/设计模式 - 空对象.md
                                        • /设计模式/设计模式 - 策略.md
                                          • /设计模式/设计模式 - 简单工厂.md
                                            • /设计模式/设计模式 - 组合.md
                                              • /设计模式/设计模式 - 装饰.md
                                                • /设计模式/设计模式 - 观察者.md
                                                  • /设计模式/设计模式 - 解释器.md
                                                    • /设计模式/设计模式 - 访问者.md
                                                      • /设计模式/设计模式 - 责任链.md
                                                        • /设计模式/设计模式 - 迭代器.md
                                                          • /设计模式/设计模式 - 适配器.md
                                                            • 一、前言
                                                              • 一、概述
                                                                • 设计模式目录

                                                                  2022年5月21日小于 1 分钟

                                                                  此页内容
                                                                  • 工厂方法(Factory Method)
                                                                    • Intent
                                                                    • Class Diagram
                                                                    • Implementation
                                                                    • JDK

                                                                  # 工厂方法(Factory Method)

                                                                  # Intent

                                                                  定义了一个创建对象的接口,但由子类决定要实例化哪个类。工厂方法把实例化操作推迟到子类。

                                                                  # Class Diagram

                                                                  在简单工厂中,创建对象的是另一个类,而在工厂方法中,是由子类来创建对象。

                                                                  下图中,Factory 有一个 doSomething() 方法,这个方法需要用到一个产品对象,这个产品对象由 factoryMethod() 方法创建。该方法是抽象的,需要由子类去实现。

                                                                  img

                                                                  # Implementation

                                                                  public abstract class Factory {
                                                                      abstract public Product factoryMethod();
                                                                      public void doSomething() {
                                                                          Product product = factoryMethod();
                                                                          // do something with the product
                                                                      }
                                                                  }
                                                                  
                                                                  public class ConcreteFactory extends Factory {
                                                                      public Product factoryMethod() {
                                                                          return new ConcreteProduct();
                                                                      }
                                                                  }
                                                                  
                                                                  public class ConcreteFactory1 extends Factory {
                                                                      public Product factoryMethod() {
                                                                          return new ConcreteProduct1();
                                                                      }
                                                                  }
                                                                  
                                                                  public class ConcreteFactory2 extends Factory {
                                                                      public Product factoryMethod() {
                                                                          return new ConcreteProduct2();
                                                                      }
                                                                  }
                                                                  

                                                                  # JDK

                                                                  • java.util.Calendaropen in new window
                                                                  • java.util.ResourceBundleopen in new window
                                                                  • java.text.NumberFormatopen in new window
                                                                  • java.nio.charset.Charsetopen in new window
                                                                  • java.net.URLStreamHandlerFactoryopen in new window
                                                                  • java.util.EnumSetopen in new window
                                                                  • javax.xml.bind.JAXBContextopen in new window
                                                                  编辑此页open in new window
                                                                  上次编辑于: 2022/5/21 13:08:59
                                                                  贡献者: yzqdev
                                                                  上一页
                                                                  /设计模式/设计模式 - 外观.md
                                                                  下一页
                                                                  /设计模式/设计模式 - 抽象工厂.md
                                                                  powered by vuepress-theme-home