As Josh Bloch writes, for business objects, “overriding the equals method [is] necessary to satisfy programmer expectations.” (Effective Java, item 7). Apart from benefits he mentions – conformance to expectations, correct use in maps and sets etc. – I’ve found that implementing equals (and hashCode) really make you consider what the classes represent. Certainly for business objects, i.e. objects in your domain model, trying to define a business-level identity for your classes is a good way of validating that you’ve correctly captured a business-relevant concept.
An equals definition can also serve as a useful piece of documentation describing your domain. Here, we’ll consider an approach that tries to do this as cleanly and conveniently as possible…declaratively! (more…)
Tags: business key, domain object, equals, hashcode, Java
Filed under Java | 4 Comments »