Implementing object equivalence in Java using the template method design pattern

  • Authors:
  • Daniel E. Stevenson;Andrew T. Phillips

  • Affiliations:
  • University of Wisconsin-Eau Claire, Eau Claire, WI;University of Wisconsin-Eau Claire, Eau Claire, WI

  • Venue:
  • SIGCSE '03 Proceedings of the 34th SIGCSE technical symposium on Computer science education
  • Year:
  • 2003

Quantified Score

Hi-index 0.00

Visualization

Abstract

A standard practice in object-oriented programming is to implement an operation, called equals in Java, for testing the equality of two objects. The equals method should be defined for every new Java class, but because of the intricacies of inheritance, casting, and dynamic typing, equals is often quite difficult to write correctly. And unfortunately many textbooks present flawed implementations of this operation. In this paper, we present a semantically correct technique for testing object equivalence, a technique that simultaneously brings together important mathematical foundations (equivalence relations), practical programming issues (inheritance, casting, dynamic typing), and sound software design (design patterns) in a natural and compelling way. While Java is used to demonstrate how the semantic flaws are corrected and the design improved using our techniques, the design is general enough that it will be clear how the same ideas could easily be extended to other languages such as C++.