什么是“静态工厂”方法?


当前回答

当您希望确保只有一个实例将返回要使用的具体类时,静态工厂方法很好。

例如,在一个数据库连接类中,您可能希望只有一个类创建数据库连接,因此如果您决定从Mysql切换到Oracle,您只需更改一个类中的逻辑,而应用程序的其余部分将使用新的连接。

如果您希望实现数据库池,那么也可以在不影响应用程序其余部分的情况下完成。

它保护应用程序的其余部分不受您可能对工厂所做的更改的影响,这就是目的。

它是静态的原因是,如果你想跟踪一些有限的资源(套接字连接或文件句柄的数量),那么这个类可以跟踪已经传递和返回了多少,这样你就不会耗尽有限的资源。

其他回答

One of the advantages of the static factory methods with private constructor(object creation must have been restricted for external classes to ensure instances are not created externally) is that you can create instance-controlled classes. And instance-controlled classes guarantee that no two equal distinct instances exist(a.equals(b) if and only if a==b) during your program is running that means you can check equality of objects with == operator instead of equals method, according to Effective java.

The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) or noninstantiable (Item 4). Also, it allows an immutable class (Item 15) to make the guarantee that no two equal instances exist: a.equals(b) if and only if a==b. If a class makes this guarantee, then its clients can use the == operator instead of the equals(Object) method, which may result in improved performance. Enum types (Item 30) provide this guarantee.

摘自Effective Java, Joshua Bloch(第1项,第6页)

我想我会在这篇文章中添加一些我所知道的东西。我们在最近的android项目中广泛使用了这种技术。除了使用new操作符创建对象,您还可以使用静态方法来实例化一个类。代码清单:

//instantiating a class using constructor
Vinoth vin = new Vinoth(); 

//instantiating the class using static method
Class Vinoth{
  private Vinoth(){
  }
  // factory method to instantiate the class
  public static Vinoth getInstance(){
    if(someCondition)
        return new Vinoth();
  }
}

静态方法支持有条件的对象创建:每次调用构造函数都会创建一个对象,但您可能不希望这样。假设你想检查一些条件,然后你想创建一个新对象。除非满足条件,否则不会每次都创建一个新的Vinoth实例。

另一个例子来自Effective Java。

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

此方法将布尔原语值转换为布尔对象引用。boolean . valueof (boolean)方法说明了我们,它从不创建对象。静态工厂方法从重复调用中返回相同对象的能力允许类在任何时候对存在的实例保持严格控制。

与构造函数不同的是,静态工厂方法可以返回返回类型的任何子类型的对象。这种灵活性的一个应用是API可以返回对象,而不需要将它们的类设为公共。以这种方式隐藏实现类可以得到一个非常紧凑的API。

Calendar.getInstance()是一个很好的例子,它根据地区创建一个佛教日历,日本帝国日历或默认的格鲁吉亚日历。

另一个例子,我认为是单例模式,你使你的构造函数私有创建一个自己的getInstance方法,你确保,总是有一个实例可用。

public class Singleton{
    //initailzed during class loading
    private static final Singleton INSTANCE = new Singleton();

    //to prevent creating another instance of Singleton
    private Singleton(){}

    public static Singleton getSingleton(){
        return INSTANCE;
    }
}

当您希望确保只有一个实例将返回要使用的具体类时,静态工厂方法很好。

例如,在一个数据库连接类中,您可能希望只有一个类创建数据库连接,因此如果您决定从Mysql切换到Oracle,您只需更改一个类中的逻辑,而应用程序的其余部分将使用新的连接。

如果您希望实现数据库池,那么也可以在不影响应用程序其余部分的情况下完成。

它保护应用程序的其余部分不受您可能对工厂所做的更改的影响,这就是目的。

它是静态的原因是,如果你想跟踪一些有限的资源(套接字连接或文件句柄的数量),那么这个类可以跟踪已经传递和返回了多少,这样你就不会耗尽有限的资源。

如果类的构造函数是私有的,那么就不能从类的外部为类创建对象。

class Test{
 int x, y;
 private Test(){
  .......
  .......
  }
}

我们不能从上面的类的外部创建一个对象。所以你不能从类外访问x y。那么这门课有什么用呢? 下面是答案:FACTORY方法。 在上面的类中添加下面的方法

public static Test getObject(){
  return new Test();
}

现在你可以在这个类的外部创建一个对象。就像……

Test t = Test.getObject();

因此,通过执行类的私有构造函数返回类对象的静态方法称为FACTORY方法。

这一切都归结为可维护性。最好的方式是,无论何时使用new关键字创建对象,都是将所编写的代码耦合到实现中。

The factory pattern lets you separate how you create an object from what you do with the object. When you create all of your objects using constructors, you are essentially hard-wiring the code that uses the object to that implementation. The code that uses your object is "dependent on" that object. This may not seem like a big deal on the surface, but when the object changes (think of changing the signature of the constructor, or subclassing the object) you have to go back and rewire things everywhere.

如今,由于依赖注入需要大量的样板代码,而这些代码本身维护起来有点困难,工厂在很大程度上被忽略了。依赖注入基本上等同于工厂,但允许你以声明的方式(通过配置或注释)指定你的对象如何连接在一起。