I was reading factory design pattern and on one of the links I read the below mentioned fact.
{ Factory pattern which is used along with various Immutable classes likes Boolean eg Boolean.valueOf() }
With this the background, can some one explain how it has been implemented in Boolean and other immutable classes. Apologies if I am missing a silly thing here.
Regards Tarun
If you see two methods below :
public static Boolean valueOf(boolean b) {
return (b ? TRUE : FALSE);
}
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
These are static methods of Boolean class and return Boolean type object based on parameter that is provided.
So, you don't create Boolean object, instead Boolean class itself creates/returns(already created) object for you. Hence factory for you.
它不会返回一个new Boolean()
,而是检查参数并返回一个现有的Boolean
对象, Boolean.TRUE
或Boolean.FALSE
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.