gpt4 book ai didi

java - 如何将从 API REST 检索到的对象分配给单例类?

转载 作者:行者123 更新时间:2023-11-30 02:44:46 26 4
gpt4 key购买 nike

我有一个单例对象,我需要分配 API REST 返回的对象,并且执行此操作时不会修改任何内容。

单例:

public class Stations {
private static Stations instance = null;
private List<Station> stations;
private Stations() {
// Exists only to defeat instantiation.
}
public static Stations getInstance() {
if(instance == null) {
instance = new Stations();
}
return instance;
}
public List<Station> getStations(){
return this.stations;
}
}

电话:

public class StationsCall implements Job {
private Stations stations = Stations.getInstance();
Client client = ClientBuilder.newClient();
public void execute(JobExecutionContext context) throws JobExecutionException {

WebTarget targetGet = client.target("http://wservice.viabicing.cat/v2/stations");
this.stations = targetGet.request(MediaType.APPLICATION_JSON_TYPE).get(new GenericType<Stations>(){});
}
public List<Station> getStations(){
List<Station> aux = this.stations.getStations();
return aux;
}
}

最佳答案

调用 this.stations = targetGet.request(...) 仅修改类 StationsCall 的字段 stations,而不会修改如果不修改实际的单例,您甚至不应该能够创建 Stations 的实例,因为它的构造函数是 private

您需要在单例中设置一个setter来设置当前的电台列表。

类似这样的事情:

public class Stations {
// Use an AtomicReference to make sure that all threads see the last list of stations
private final AtomicReference<List<Station>> stations = new AtomicReference<>();
private Stations() {
// Exists only to defeat instantiation.
}
public static Stations getInstance() {
// Lazy create your instance of Stations using a static inner class
// for thread safety
return StationsHolder.INSTANCE;
}
public List<Station> getStations(){
// Get the last list of stations from the AtomicReference
return this.stations.get();
}

public void setStations(List<Station> stations) {
// Set the new list of stations and make it unmodifiable for thread safety
this.stations.set(Collections.unmodifiableList(stations));
}

private static class StationsHolder {
private static final Stations INSTANCE = new Stations();
}
}

注意:我改进了您的代码,使其成为线程安全的,因为单例很有可能被并发线程使用。

那么你的类StationsCall将是:

public void execute(JobExecutionContext context) throws JobExecutionException {
...
Stations.getInstance().setStations(targetGet.request(...));
}

public List<Station> getStations(){
return Stations.getInstance().getStations();
}
<小时/>

假设您真正需要的是能够获取当前的电台列表以仅集中其访问,并且您不关心它是否是单例,那么您的代码应该是:

public class Stations {
// Use an AtomicReference to make sure that all threads see the last instance
private static final AtomicReference<Stations> INSTANCE =
new AtomicReference<>(new Stations());
private List<Station> stations;

public Stations() {
}

public Stations(final List<Station> stations) {
// Make a safe and unmodifiable copy of the list of stations
this.stations = Collections.unmodifiableList(new ArrayList<>(stations));
}

public static Stations getInstance() {
return INSTANCE.get();
}

public List<Station> getStations(){
return this.stations;
}

public static void setInstance(Stations stations) {
// Set the new instance
INSTANCE.set(new Stations(stations.getStations()));
}
}

那么你的类StationsCall将是:

public void execute(JobExecutionContext context) throws JobExecutionException {
...
Stations.setInstance(targetGet.request(...));
}

public List<Station> getStations(){
return Stations.getInstance().getStations();
}

关于java - 如何将从 API REST 检索到的对象分配给单例类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40527061/

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