在更新对象时更新对对象的所有引用(对引用的引用) [英] Update all references to an object when the object is updated(reference to a reference)

查看:134
本文介绍了在更新对象时更新对对象的所有引用(对引用的引用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中声明一个类似于C ++中的指针的对象,让我向您展示一个示例:

I want to declare an object in java that is like a a pointer to a pointer in C++, let me show you an example :

//*** At the application startup

//Initialize a settings container class
Settings settings = //Load settings 

//Declaring static Application class that contain a reference to the settings container
Application.setSettings(settings);

//Get sub settings from settings container class
DatabaseSettings dbSettings =  settings.getDbSettings();
LogSettings logSettings = settings.getLogSettings();

//Initialize components 
DatabaseConnector dbConn = new DatabaseConnector(dbSettings);
Logger logger = new Logger(logSettings);

在上面的代码中,我创建了一个包含我的应用程序组件的所有设置的设置容器类,然后我将每个子设置类分配给了相关组件,此时,在程序执行过程中,我想更新设置,并让应用程序的组件查看更新的设置,因此例如在执行过程中,我可以通过以下方式更新设置容器:

In the above code i have created a settings container class that contain all settings of my application components, then i have assigned each sub settings class to the related component, at this point during the execution of the program i want to update the settings and let the component of the application to see the updated settings, so for example during the execution i can update the settings container in this way :

//Update settings through Application static class 
Settings newSettings = //assign updated settings
Application.setSettings(newSettings);

现在的问题是,当我在运行时更新设置容器时,应用程序静态类将包含对newSettings实例的更新引用,而每个子设置实例均继续引用旧的子设置,因此:

Now the problem is that when i update the settings container at runtime the application static class will contain the updated reference to the newSettings instance while each sub Settings instance keep referencing the old sub settings, so :

dbConn      ---> settings.getDbSettings()
logSettings ---> settings.getLogSettings()

虽然我希望两个引用自动引用设置的新实例,所以:

While i want that the two reference automatically reference to the new instance of the settings, so :

dbConn      ---> newSettings.getDbSettings()
logSettings ---> newSettings.getLogSettings()

这就像一个指向指针的指针...在Java中可能吗?怎么做到呢 ?

It is like a pointer to pointer ... Is it possible in Java ? How can it be done ?

推荐答案

在Java中,您仅具有对对象的引用.要获得对引用的引用,您需要某种间接方式.例如

In Java, you only have references to objects. To have a reference to a reference you need some sort of indirection. e.g.

AtomicReference<DatabaseSettings> refDatabaseSettings = new AtomicReference<DatabaseSettings>();

refDatabaseSettings.set(dbSettings);

DatabaseSettings dbSettings = refDatabaseSettings.get();

您可以传递参考,并将其更改为一个位置.但是,这不会通知任何引用已更改,只是允许所有引用在下次检查时看到相同的内容.

You can pass the ref around and change it in one place. However, this won't notify any of the references that it has changed, just allow all references to see the same thing when they check next time.

这篇关于在更新对象时更新对对象的所有引用(对引用的引用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆