简体   繁体   English

rt.jar中的工厂设计模式实现

[英]Factory design pattern implementation in rt.jar

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() } {工厂模式与各种不可变类一起使用,例如布尔值,例如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.TRUEBoolean.FALSE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM