gpt4 book ai didi

java - 设计模式名称: Is it a factory?

转载 作者:行者123 更新时间:2023-12-01 06:48:04 24 4
gpt4 key购买 nike

下面的类显示了与真实用例类似的内容。它始终为同一线程返回相同的实例。

public class LookingForName {

private static final ThreadLocal<Something> threadLocal =
new ThreadLocal<Something>(){
@Override
protected Something initialValue() {
return getSomethingSpecial(); // not relevant
}
};

/**
* @return always the same instance of "Something" for the current thread.
*/
public static Something getInstance() {
return threadLocal.get();
}

}

你会怎么调用它?是“工厂”吗? “值(value)持有者”? “ThreadLocalStore”?

最佳答案

有些人简单地将其称为 ThreadLocal Pattern 。另一个已知名称是 Thread-local Storage (TLS) .

关于java - 设计模式名称: Is it a factory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3457753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com