什么是“静态工厂”方法?
当前回答
这一切都归结为可维护性。最好的方式是,无论何时使用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.
如今,由于依赖注入需要大量的样板代码,而这些代码本身维护起来有点困难,工厂在很大程度上被忽略了。依赖注入基本上等同于工厂,但允许你以声明的方式(通过配置或注释)指定你的对象如何连接在一起。
其他回答
如果类的构造函数是私有的,那么就不能从类的外部为类创建对象。
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.
如今,由于依赖注入需要大量的样板代码,而这些代码本身维护起来有点困难,工厂在很大程度上被忽略了。依赖注入基本上等同于工厂,但允许你以声明的方式(通过配置或注释)指定你的对象如何连接在一起。
静态
用关键字'static'声明的成员。
工厂方法
方法,用于创建和返回新对象。
在Java中
编程语言与“静态”的含义相关,但与“工厂”的定义无关。
注意!“静态工厂方法与工厂方法模式不同”(c) Effective Java, Joshua Bloch。
工厂方法:定义一个用于创建对象的接口,但是让实现该接口的类来决定实例化哪个类。Factory方法允许类延迟实例化到子类“(c) GoF”。
静态工厂方法只是一个返回类实例的静态方法。(c)有效的Java,约书亚·布洛赫。通常这个方法在一个特定的类中。
的区别:
The key idea of static factory method is to gain control over object creation and delegate it from constructor to static method. The decision of object to be created is like in Abstract Factory made outside the method (in common case, but not always). While the key (!) idea of Factory Method is to delegate decision of what instance of class to create inside Factory Method. E.g. classic Singleton implementation is a special case of static factory method. Example of commonly used static factory methods:
返回对象的值 getInstance newInstance
静态工厂方法模式是封装对象创建的一种方式。如果没有工厂方法,只需直接调用类的构造函数:Foo x = new Foo()。使用这种模式,你可以调用工厂方法:Foo x = Foo.create()。构造函数被标记为private,因此只能从类内部调用它们,而工厂方法被标记为静态,因此可以在没有对象的情况下调用它。
这种模式有几个优点。一个是工厂可以从许多子类(或接口的实现者)中选择并返回。通过这种方式,调用者可以通过参数指定所需的行为,而不必知道或理解潜在的复杂类层次结构。
Another advantage is, as Matthew and James have pointed out, controlling access to a limited resource such as connections. This a way to implement pools of reusable objects - instead of building, using, and tearing down an object, if the construction and destruction are expensive processes it might make more sense to build them once and recycle them. The factory method can return an existing, unused instantiated object if it has one, or construct one if the object count is below some lower threshold, or throw an exception or return null if it's above the upper threshold.
根据维基百科上的文章,多个工厂方法还允许对相似的参数类型进行不同的解释。通常构造函数与类具有相同的名称,这意味着具有给定签名的构造函数只能有一个。工厂没有那么多限制,这意味着你可以有两个不同的方法接受相同的参数类型:
Coordinate c = Coordinate.createFromCartesian(double x, double y)
and
Coordinate c = Coordinate.createFromPolar(double distance, double angle)
Rasmus指出,这也可以用来提高可读性。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap