什么是“静态工厂”方法?
当前回答
注意!“静态工厂方法与工厂方法模式不同”(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
其他回答
可读性可以通过静态工厂方法来提高:
比较
public class Foo{
public Foo(boolean withBar){
//...
}
}
//...
// What exactly does this mean?
Foo foo = new Foo(true);
// You have to lookup the documentation to be sure.
// Even if you remember that the boolean has something to do with a Bar
// you might not remember whether it specified withBar or withoutBar.
to
public class Foo{
public static Foo createWithBar(){
//...
}
public static Foo createWithoutBar(){
//...
}
}
// ...
// This is much easier to read!
Foo foo = Foo.createWithBar();
如果类的构造函数是私有的,那么就不能从类的外部为类创建对象。
class Test{
int x, y;
private Test(){
.......
.......
}
}
我们不能从上面的类的外部创建一个对象。所以你不能从类外访问x y。那么这门课有什么用呢? 下面是答案:FACTORY方法。 在上面的类中添加下面的方法
public static Test getObject(){
return new Test();
}
现在你可以在这个类的外部创建一个对象。就像……
Test t = Test.getObject();
因此,通过执行类的私有构造函数返回类对象的静态方法称为FACTORY方法。
Java实现包含实用程序类Java .util. arrays和Java .util. collections,它们都包含静态工厂方法,它的示例和如何使用:
arrays . aslist(“1”、“2”、“3”) Collections.synchronizedList(..), Collections.emptyList(), Collections.unmodifiableList(…)(只是一些例子,可以检查javadocs mor方法的例子https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html)
java.lang.String类也有这样的静态工厂方法:
String.format(…),String.valueOf (. .), String.copyValueOf(…)
注意!“静态工厂方法与工厂方法模式不同”(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
我想我会在这篇文章中添加一些我所知道的东西。我们在最近的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;
}
}
推荐文章
- 在流中使用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