gpt4 book ai didi

java - 将 setter 添加到单例类

转载 作者:行者123 更新时间:2023-12-02 10:17:12 25 4
gpt4 key购买 nike

我有一个像这样的单例类

public class Service{

// SingletonHolder is a container class to hold singleton instance
private static final SingletonHolder<A> singleton = new SingletonHodler<>(new Service());

private Service(){
}
public static Service getInstance(){

//.instance() is a method in SingletonHolder to return singleton instance
return singleton.instance();
}

//Method to start the Service class
public void start(){
// start the service
// get the initial configuration and use the configuration value to speify a URL, something like:
String initialConfiguration = Configuration.getSettings();
TargetUrl = initialConfiguration.get.......
}

}

另一个用于配置的类。该类将初始化配置并使用configurationUpdate()来获取更新的配置值。

public class Configuration{


public void configurationInitialize(){

// initialize the configuration and get the value
initialConfigValue = ..........
}


// Method to update configuration
public void configurationUpdate(){

// some mehtod which will get the updated configuration value
String updateConfiguration = .............

}
//method to retrun configuration settings
public static String getSettings(){.........}
}

现在我想先获得初始配置。然后,如果配置发生更改,则获取updatedValue。

我的选择之一是向 Service 类添加一个 setter 方法。并调用Configuration类的configurationUpdate()方法中的setter方法,将UpdateVlaue传递给该setter。

我不确定这是否是正确的方法。如果加上setter方法,还是单例吗?这个操作会不会有什么问题呢?非常感谢!!!

编辑:另一个初始化类将调用配置类来初始化配置,同时也会调用Service.getInstance().start()

最佳答案

I am not sure if it is the proper way to do that. If I add the setter method, is it still a singleton? Will this operation cause any problem?

它仍然是一个单例。如果所有线程都应使用相同的 targetUrl 那么应该没问题。如果每个线程都应该使用它自己的 targetUrl,那么更改 targetUrl 字段的值的 setter 将影响所有线程。

不确定这是否是所需要的,但也许它给出了一些有用的提示。

不要将配置传递给服务实例,而是通过 ThreadLocal 使配置可用。和一个静态方法,以便 Service 类可以在需要时检索配置(请参阅 Configuration#settingsHolder 变量和 Configuration#getSettings 方法)

注意:我没有使用 SingletonHolder,因为我不清楚它的目的(它的描述方式不提供延迟初始化,通常实例持有者用于延迟初始化目的)

public class Service {

private static final Service INSTANCE = new Service();

private static final ThreadLocal<TargetUrl> TARGET_URL_HOLDER = new ThreadLocal();

private Service() {

}

public static Service getInstance() {
return Service.INSTANCE;
}

public void start() {
Settings initialConfiguration = Configuration.getSettings();
TARGET_URL_HOLDER.set(initialConfiguration.getTargetUrl());
// some more stuff before starting
}

public void reinitialize() {
Settings updatedConfiguration = Configuration.getSettings();
TARGET_URL_HOLDER.set(updatedConfiguration.getTargetUrl());
.......
}
}

public class Settings {
private TargetUrl targetUrl;

public TargetUrl getTargetUrl() {
return this.targetUrl;
}

public void setTargetUrl(TargetUrl targetUrl) {
this.targetUrl = targetUrl;
}
}

public class Configuration {
// use threadLocal to store the settings instance so it can be retrieved via a static method (e.g. getSettings)
private static ThreadLocal<Settings> SETTINGS_HOLDER = new ThreadLocal<>();

public void initialize() {

// initialize the configuration
Settings initialSettings = ..........
SETTINGS_HOLDER.set(initialSettings);
}

// Method to update configuration
public void update() {
// some method which will get the updated configuration value
Settings updatedSettings = .............
SETTINGS_HOLDER.set(updatedSettings);
}

//method to retrun configuration settings
public static Settings getSettings() {
return SETTINGS_HOLDER.get();
}
}

public class Test {

public static void main(String[] args) {
Configuration configuration = new Configuration();
configuration.initialize();

Service service = Service.getInstance();
service.start();
// use service instance
...................

configuration.update();

service.reinitialize();

// keep using the service instance
.................
}
}

根据用例,如果所有线程必须使用相同的 targetUrl,则也可以使用 AtomicReference 类型的静态变量来实现。而不是 ThreadLocal 类型的静态变量

关于java - 将 setter 添加到单例类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54600431/

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