質問

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 e.g. 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.

他のヒント

Instead of returning a new Boolean(), it will check the parameter and return one of the existing Boolean objects, Boolean.TRUE or Boolean.FALSE.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top