实例工厂方法Vs静态工厂方法 [英] instance factory methods Vs Static factory methods

查看:186
本文介绍了实例工厂方法Vs静态工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有工厂方法都不能是静态的吗?产生产品的东西需要陈述吗?何时适合实例工厂或静态工厂方法?你能否举例说明两者的区别?

Can't all factory methods be static ? Does something that produces a product need state ? When is it appropriate to go for a instance factory or static factory method ? Can you provide me examples differentiating the two ?

推荐答案

假设通过实例工厂方法你实际上是在谈论GoF 工厂方法,术语静态工厂方法由Joshua Bloch在其着作Effective Java中描述。谷歌搜索我到达这些网站:

Assuming that by "instance factory method" you're actually saying about the GoF "factory method", the term "static factory method" is described by Joshua Bloch in his book "Effective Java". Googling around I reached at these sites:

  • Factory Method: http://sourcemaking.com/design_patterns/factory_method
  • Static Factory Method: http://www.informit.com/articles/article.aspx?p=1216151

希望它有助于使差异更加清晰。

Hope that it helped to make the difference a little bit clearer.

遵循Marvo的建议:

Following Marvo's advice:


  • GoF中所述的工厂方法:

  • Factory Method as stated in GoF:

定义用于创建对象,但让子类决定实例化哪个类。 Factory Method允许类将实例化延迟到子类。

define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.


示例(Java代码):

Example (Java code):

    public abstract class Product { ... }

    public class ConcreteProduct extends Product { ... }

    public abstract class Creator {
      public void anOperation() { ... product = factoryMethod(); ... }
      public abstract Product factoryMethod();
    }

    public class ConcreteCreator extends Creator {
      public Product factoryMethod() { return new ConcreteProduct(); }
    }




  • 有效Java中所述的静态工厂方法:

    • Static Factory Method as stated in Effective Java:


      类可以提供公共静态工厂方法,它只是一个返回类实例的静态方法。 (...)静态工厂方法的一个优点是,与构造函数不同,它们具有名称。 (...)静态工厂方法的第二个优点是,与构造函数不同,它们不需要在每次调用时创建新对象。 (...)静态工厂方法的第三个优点是,与构造函数不同,它们可以返回其返回类型的任何子类型的对象。 (...)仅提供静态工厂方法的主要缺点是没有公共或受保护构造函数的类不能被子类化。

      A class can provide a public static factory method, which is simply a static method that returns an instance of the class. (...) One advantage of static factory methods is that, unlike constructors, they have names. (...) A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked. (...) A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type. (...) The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.


    • 示例(Java代码):

      Example (Java code):

          public class Boolean {
            ...
            public static Boolean valueOf(boolean b) {
              return b ? Boolean.TRUE : Boolean.FALSE;
            }
            ...
          }
      

      这篇关于实例工厂方法Vs静态工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆