确保同一个对象不会从 Java 中的 XML API 加载两次

发布于 2024-11-02 20:18:43 字数 378 浏览 1 评论 0原文

我是 Java 新手,正在开发公共交通 Java 应用程序作为第一个小项目。

我通过 XML API(使用 DOM XML API)从服务器加载传输数据。因此,当您调用 BusStop(int id) 的构造函数时,构造函数会根据提供的 id 从服务器加载有关该 Stop 的信息。所以,我想知道以下几件事:如何确保不会实例化具有相同 id 的两个 BusStop 对象(我只想要每个 BusStop 一个对象)?

另外,是否有人对我应该如何加载对象有建议,这样我就不需要每次运行应用程序时加载整个数据库,只需加载 BusStop 以及该站点的相关 Arrivals 和 BusTrips 对象?我以前做过 C++ 和 MVC PHP 编程,但没有经历过使用循环对象引用等加载大量对象。

谢谢!

I am new to Java, and am working on a Public Transit Java app as a first small project.

I am loading transit data in from a server through an XML api (using the DOM XML API). So when you call a constructor for say a BusStop(int id), then the constructor loads the info about that Stop from the server based on the id provided. So, I am wondering about a couple things: how can I make sure I don't instantiate two BusStop objects with the same id (I just want one object for each BusStop)?

Also does anyone have recommendations on how I should load up the objects, so I don't need to load the whole database every time I run the app, just the BusStop, and relevant Arrivals and BusTrips objects for that stop? I have done C++ and MVC PHP programming previously, but haven't had experienced loading large numbers of objects with circular object references etc.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-11-09 20:18:44

对于每个给定 id 只加载一次的类,请使用某种工厂设计模式。在内部,您可能希望将 id 存储到 Map 中的实例映射。在实际从服务器获取数据之前,首先在此映射上进行查找,看看是否已经为此 id 加载了一个类。如果没有,则继续获取并更新地图。

For the classes you want to load only once per given id, use some kind of Factory design pattern. Internally you may want to store id to instance mapping in a Map. Before actually fetching the data from server, first do a lookup on this map to see if you already have a class loaded for this id. If not then go ahead with fetching and the update the map.

请别遗忘我 2024-11-09 20:18:43

我不会在构造函数中启动下载/反序列化过程。我会为每个实体类型编写一个管理器类,其中包含一个根据给定实体的 ID 获取 Java 对象的方法。使用 HashMap,其中键类型作为实体 ID,值类型作为该对象的 Java 类。管理器将是使用您首选模式的单例(为了简单起见,我可能会使用静态成员)。

fetch 方法应该做的第一件事是检查映射以查看它是否包含给定 ID 的条目。如果它已经获取并构建了该对象,则返回它。如果没有,则从远程服务获取实体,适当地反序列化该对象,通过指定的 ID 将其加载到 HashMap 中,然后返回它。

关于对其他对象的引用,我建议您将它们表示为 Java 对象中的 ID,而不是将它们存储为 Java 对象引用并与引用对象同时反序列化它们。应用程序可以通过相关管理器按需延迟实例化这些对象。这减少了循环引用带来的问题。

如果数据量可能超过 JVM 上的可用 RAM,您需要考虑定期从映射中删除旧对象以恢复内存(确信它们会在需要时重新加载)。

I wouldn't start the download/deserialization proces in a constructor. I would write a manager class per entity type with a method to fetch a Java object for a given entity based on its ID. Use a HashMap with the key type as your entity ID and the value type as the Java class for that object. The manager would be a singleton using your preferred pattern (I would probably use static members for simplicity).

The first thing the fetch method should do is check the map to see if it contains an entry for the given ID. If it has already fetched and build this object, return it. If it has not, fetch the entity from the remote service, deserialize the object appropriately, load it into the HashMap by the specified ID, and return it.

Regarding references to other object I suggest you represent those as IDs in your Java objects rather than storing them as Java object references and deserializing them at the same time as the referencing object. The application can lazily instantiate those objects on demand through the relevant manager. This reduces problems through circular references.

If the amount of data is likely to exceed available RAM on your JVM you'd need to consider periodically removing older objects from the map to recover memory (confident they would be reloaded when required).

绮烟 2024-11-09 20:18:43

对于此应用程序,我将使用以下 Java EE 技术:JAX-RS、JPA 和 JAXB。您会发现几乎每个 Java 应用程序服务器(即 GlassFish)中都包含这些技术。

JPA - Java Persistence API

提供一种将对象与数据库相互转换的简单方法。通过注释,您可以将关系标记为惰性关系,以防止读取整个数据库。还可以通过使用缓存来减少数据库访问和对象创建。

JAXB - XML 绑定的 Java 体系结构

提供将对象与 XML 相互转换的简单方法。 Java SE 6 中包含一个实现。

JAX-RS - 用于 RESTful 服务的 Java API

提供一个简单的 API(通过 HTTP)用于与 XML 交互。

示例

您可以查看我发布到我的博客上的示例:

For this application I would use the following Java EE technologies: JAX-RS, JPA and JAXB. You will find these technologies included in almost every Java application server (i.e. GlassFish).

JPA - Java Persistence API

Provides a simple means of converting your objects to/from the database. Through annotations you can mark a relationship as lazy to prevent the entire database from being read. Also through the use of caching database access and object creation is reduced.

JAXB - Java Architecture for XML Binding

Provides a simple means of converting your objects to/from XML. An implementation is included in Java SE 6.

JAX-RS - Java API for RESTful Services

Provides a simple API (over HTTP) for interacting with XML.

Example

You can check out an example I posted to my blog:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文