我有这样的单例课程
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()
答案 0 :(得分:1)
我不确定这是否是正确的方法。如果添加setter方法,它仍然是单例吗?此操作会引起任何问题吗?
仍然是单身人士。如果所有线程都应使用相同的targetUrl,则应该没问题。如果每个线程都应使用自己的targetUrl,则更改targetUrl字段值的设置器将影响所有线程。
不确定这是否是必需的,但也许会给出一些有用的提示。
不是通过配置将其传递给服务实例,而是通过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类型的静态变量
来实现它。