Search code examples
oopclass-designstatic-methodsooadclass-method

OOP Design: Where to put object specific "compare" method?


I have some measurement object instances from a series of test runs stored in a test collection object. I also have some logic that can compare two test result object instances and tell me if they are "close enough".

Where should this logic be placed?

  1. On the object as a method? Like: instance.approximately_equal(other)
  2. On the object's class as a class/static method? class.approximately_equal(a,b)
  3. On the collection object as a method? collection.approximately_equal(a,b)

What is the correct OO design for this?

(I ask, since although #1 would seem the correct solution, I'd never be asking if some one instance is approximately_equal to a different instance. Only if "some group of objects" are equal to each other. It got me thinking...)

Thanks


Solution

  • The object oriented design books I have read suggest putting cross class functionality into service provider objects. This will decouple the two objects and reduce complexity, but may be overkill if your project is small.