문제

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