对于相等但不相同的对象该怎么办?

发布于 2025-01-13 20:56:59 字数 1004 浏览 0 评论 0 原文

这要么是一个Java编码设计问题,要么是一个领域建模问题,我还不确定!

背景,简化,因为我无法分享所有细节:

我正在开发的系统部署在多个实例中(例如,在不同的客户处,并且在一个客户处可能有开发、测试、预生产、生产实例)

系统配置是 ConfigArtefact 列表,其中 T 表示它可能是数据库连接配置,或者预定义查询,或者......

ConfigArtefacts 被命名。这些名称在语义上有意义/众所周知(例如,可能存在“Console.translations.en”、“Console.translations.fr”的对象)或“Application.Database.connection.credentials”、“Reporting.Database.connection” .credentials")并且对于每个部署都是不同的 - 对于给定的部署,没有两个不同的 ConfigArtefacts 具有相同的名称

ConfigArtefacts 具有其他属性(例如数据库、用户名和密码)取决于 使用的类型。在该系统的不同部署中,属性的值可能不同。

人工制品没有自然的排序,即使是相同类型的。如果需要任意排序,我会使用名称。

目标:

我需要编写一些内容来比较该系统的两个部署的配置,并识别已添加、删除或更改的工件。为了在每次部署中找到相同的工件,我只需要按名称进行比较(我总是知道我正在使用什么类型的工件)。为了判断它们是否发生了变化,我需要通过所有其他属性进行比较。

所以,有两种比较。其中一个可以使用 equals/hashcode 进行建模,但另一个则不行。哪个应该使用 equals()? (我认为按名称命名的一个,然后添加和删除只是设置减法,使用许多集合库之一)。

这会是正常的选择吗?如果是这样,另一个(“全面比较”)是否有一个约定的名称?我正在考虑 identicalTo() (因此,如果 one.identicalTo(two) 为 false,则两个对象将被更改)

This is either a Java coding design question, or a domain modelling question, I'm not sure yet!

Background, simplified as I can't share all the detail:

The system I'm working on is deployed in multiple instances (e.g. at different customers, and at one customer there may be development, test, preprod, prod instances)

The system configuration is a list of ConfigArtefact<T>, where T indicates that it might be a database connection configuration, or a predefined-query, or....

ConfigArtefacts are named. The names are semantically meaningful/well-known (e.g there could be an object for "Console.translations.en", "Console.translations.fr") or "Application.Database.connection.credentials", "Reporting.Database.connection.credentials") and are distinct for each deployment- no two different ConfigArtefacts will have the same name for a given deployment

ConfigArtefacts have other attributes (e.g. for the database, username and password) depending on the type used for <T>. The value of the attributes could be different in different deployments of this system.

There's no natural ordering of artefacts, even ones of the same type <T>. Where some arbitrary ordering is needed, I use the name.

Goal:

I need to write something that compares the configuration of two deployments of this system and identify Artifacts that have been added, removed, or changed. In order to find the same artefact on each deployment, I need to compare by name only (I always know what type of artefact I'm working with). In order to say if they've changed, I need to compare by all other attributes.

So, two kinds of comparison. One can be modelled with equals/hashcode, but not the other. Which should use equals()? (I think the one by name, as then added and deleted are just set subtraction, using one of the many collection libraries).

Would that be the normal choice? And if so, is there a conventional name for the other ("full compare") one? I'm considering identicalTo() (so two objects are changed if one.identicalTo(two) is false)

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

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

发布评论

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

评论(1

烟凡古楼 2025-01-20 20:56:59

您的设计有缺陷 - 您仅通过比较名称来滥用 equals()

如果必须比较所有属性才能知道对象是否已更改,那么具有相同名称的对象就不是“相等”,因为使用“更改”一词意味着存在差异,如果存在差异,则它们并不平等。

使用标识符查找某物与如果两个对象具有相同标识符则相等不同。

使用所有与确定对象是否“不同”相关的属性来实现 equals()hashCode()

为了方便和提高性能,请使用名称作为键为每个环境填充 Map>

找出 2 个这样的映射之间的差异是一项相当简单的 O(n) 任务。

Your design is flawed - you have misused equals() by only comparing name.

If all attributes must be compared to know if the object has changed, then it is not true that objects with the same name are “equal”, because the use of the word “changed” implies there’s a difference, and if there’s a difference they’re not equal.

Finding something by using an identifier is different to two objects being equal if they have the same identifier.

Implement equals() and hashCode() using all attributes that matter for determining if an object is “different”.

To facilitate convenience and performance, populate a Map<String, ConfigArtefact<?>> for each environment using the name as the key.

Finding differences between 2 such maps is a fairly trivial O(n) task.

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